Last active
August 29, 2015 14:03
-
-
Save doritostains/94b3080f6a0d7743d408 to your computer and use it in GitHub Desktop.
Replaces all values in a map with the results of a function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.walk :as walk]) | |
(defn update-values | |
"Replaces all values in a map with the results of a function" | |
[m f & args] | |
(let [f (fn [[k v]] (if (map? v) [k v] [k (apply f v args)]))] | |
(walk/prewalk (fn [x] (if (map? x) (into {} (map f x)) x)) m))) | |
(update-values {:a 1 :b 1} + 10) | |
; => {:b 11, :a 11} | |
(update-values {:a 1 :b 1 :c { :d 2 :e { :f 3 :g 4 }}} str) | |
; => {:c {:e {:g "4", :f "3"}, :d "2"}, :b "1", :a "1"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment