Mapping a function on the values of a map in Clojure

前端 未结 11 1435
面向向阳花
面向向阳花 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条回答
  •  [愿得一人]
    2020-11-29 16:40

    Here's a fairly idiomatic way to do this:

    (defn map-function-on-map-vals [m f]
            (apply merge
                   (map (fn [[k v]] {k (f v)})
                        m)))
    

    Example:

    user> (map-function-on-map-vals {1 1, 2 2, 3 3} inc))
    {3 4, 2 3, 1 2}
    

提交回复
热议问题