While on the topic, I'd like to raise a naming issue.
The 'mapmap' function seems to be a recurring theme (see, e.g.,
http://tech.puredanger.com/2010/09/24/meet-my-little-friend-mapmap/)
and many Clojure projects include one -- Incanter comes to mind. My
project used to, too. But we found out that the name "mapmap" has its
deficiencies.
First, it conflates the two senses of the word "map" (1. noun -- an
object implementing IPersistentMap; 2. verb -- to apply a function to
each element of a collection, producing a new one) by putting them
right next to each other. This impairs code readability: I once found
myself typing (mapmap #(map ... ) ...) -- it's unobvious at first
sight which "map" is used in which sense.
Second, all these implementations of mapmap floating around are
mutually incompatible, which partly stems from the fact that it is
unclear from the name what the argument of mapmap should be. Should it
be a value->value function? Or should it take two arguments, a key and
a value? Or should it take a [key value] pair?
Actually, each one of these flavors is useful on different occasions.
So to differentiate between them, we've now dropped mapmap and use
transform-v and transform-kv instead. Here they are, with docstrings:
(defn transform-kv
"Transforms each entry in a map with f and returns the resulting
map.
f should take and return a key/value pair. Similar to
clojure.contrib.generic.functor/fmap, but can take keys into account.
Works on any seq of pairs (not necessarily maps), always returning
maps."
[f x]
(into {} (map (fn [[k v]] (f k v)) x)))
(defn transform-v
"Like transform-kv, but takes a value->value function, leaving keys
intact."
[f x]
(into {} (map (fn [[k v]] [k (f v)]) x)))
--
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