Created
July 27, 2020 13:46
-
-
Save gerritjvv/c41f67050dad8804010b657474971f21 to your computer and use it in GitHub Desktop.
Apply a function walking multiple levels of maps and vectors
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 :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