Hi,
Am 10.08.2010 um 20:56 schrieb Alan:
> which is exactly what I want. But I have failed countless times to
> define a make-hand function - I'd like to be able to call (make-
> hand :west {:spade [9 5], :club [10]}) and get back
>
> {:spade
> ({:suit :spade, :owner :west, :rank 9}
> {:suit :spade, :owner :west, :rank 5}),
> :club
> ({:suit :club, :owner :west, :rank 10})}
>
> Can someone put me on the right track? I'm sure the solution for this
> is trivially simple but I can't quite find it.
So from your description the function signature should look like this:
(defn make-hand
[owner suit-map]
...)
Now we have to fill the dots. Let's have a look at the map. It already has the
right structure: it maps suits to things. In the input it's the vector of
ranks, in the output it should be the sequence of card maps. We have to do it
for all contained suits. And then pack everything back into a map.
(defn make-hand
[owner suit-map]
(->> suit-map
(map (fn [[suit ranks]]
[suit (map (partial struct-map card
:suit suit
:owner owner
:rank)
ranks)]))
(into (empty suit-map))))
Another possibility is to gradually transform the input map, which is not a
problem due to Clojure's persistent data structures.
(defn make-hand
[owner suit-map]
(reduce (fn [suit-map suit]
(update-in suit-map [suit]
(fn [ranks]
(map (partial struct-map card
:suit suit
:owner owner
:rank)
ranks))))
suit-map
(keys suit-map)))
Hope that helps.
Sincerely
Meikel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en