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#)])))
Written by Cody Rioux
Related protips
1 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
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Clojure
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#