Mapping a function on the values of a map in Clojure

前端 未结 11 1465
面向向阳花
面向向阳花 2020-11-29 16:03

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

11条回答
  •  Happy的楠姐
    2020-11-29 16:27

    Here is a fairly typical way to transform a map. zipmap takes a list of keys and a list of values and "does the right thing" producing a new Clojure map. You could also put the map around the keys to change them, or both.

    (zipmap (keys data) (map #(do-stuff %) (vals data)))
    

    or to wrap it up in your function:

    (defn map-function-on-map-vals [m f]
        (zipmap (keys m) (map f (vals m))))
    

提交回复
热议问题