I want to transform one map of values to another map with the same keys but with a function applied to the values. I would think there was a function for doing this in the c
I'm a Clojure n00b, so there may well be much more elegant solutions. Here's mine:
(def example {:a 1 :b 2 :c 3 :d 4})
(def func #(* % %))
(prn example)
(defn remap [m f]
(apply hash-map (mapcat #(list % (f (% m))) (keys m))))
(prn (remap example func))
The anon func makes a little 2-list from each key and its f'ed value. Mapcat runs this function over the sequence of the map's keys and concatenates the whole works into one big list. "apply hash-map" creates a new map from that sequence. The (% m) may look a little weird, it's idiomatic Clojure for applying a key to a map to look up the associated value.
Most highly recommended reading: The Clojure Cheat Sheet .