Skip to content

Instantly share code, notes, and snippets.

@doritostains
Last active August 29, 2015 14:03
Show Gist options
  • Save doritostains/94b3080f6a0d7743d408 to your computer and use it in GitHub Desktop.
Save doritostains/94b3080f6a0d7743d408 to your computer and use it in GitHub Desktop.
Replaces all values in a map with the results of a function
(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