Skip to content

Instantly share code, notes, and snippets.

@gerritjvv
Created July 27, 2020 13:46
Show Gist options
  • Save gerritjvv/c41f67050dad8804010b657474971f21 to your computer and use it in GitHub Desktop.
Save gerritjvv/c41f67050dad8804010b657474971f21 to your computer and use it in GitHub Desktop.
Apply a function walking multiple levels of maps and vectors
(require '[clojure.walk :refer [postwalk]])
(defn mapvals [f m]
(let [v-f (fn [v]
(cond
(map? v) v
(coll? v) (into (empty v) (map f) v)
:else (f v)))
kv-f (fn [[k v]] [k (v-f v)])]
(postwalk
(fn [x] (if (map? x) (into {} (map kv-f x)) x))
m)))
(comment
(mapvals inc {:a {:b [1 2 3]} :c 2})
; {:a {:b [2 3 4]}, :c 3}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment