Mapping a function on the values of a map in Clojure

前端 未结 11 1497
面向向阳花
面向向阳花 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:45

    Clojure 1.7 (released June 30, 2015) provides an elegant solution for this with update:

    (defn map-function-on-map-vals [m f]
        (map #(update % 1 f) m))
    
    (map-function-on-map-vals {:a "test" :b "testing"} #(.toUpperCase %))
    ;; => ([:a "TEST"] [:b "TESTING"])
    

提交回复
热议问题