Last Updated: July 19, 2020
·
778
· codyrioux

Clojure - map fn to map

This fn/macro will map a function to each value in a map, returning a map containing the corresponding keys to the new values.

; As a function, thanks to signalpillar's comment below
(defn map-fn-to-map [f m]
  "Maps f to each value of m, returning the corresponding map."
  (into {} (for [[k v] m] [k (f v)])))

; As a macro
(defmacro map-fn-to-map [f m]
  "Maps f to each value of m, returning the corresponding map."
  `(into {} (for [[k# v#] ~m] [k# (~f v#)])))

1 Response
Add your response

@signalpillar Hey! Thanks for the comment. :)

I had originally written this as a function but experienced some issues, that said it was much earlier in my Clojure career and I can't seem to recreate the issue now. So I believe you are right, it would be better to write this as a function.

I'll update accordingly.

over 1 year ago ·