Skip to content

Instantly share code, notes, and snippets.

@adreyer
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save adreyer/184d11209955f4c0ccbf to your computer and use it in GitHub Desktop.

Select an option

Save adreyer/184d11209955f4c0ccbf to your computer and use it in GitHub Desktop.
Is there a clojure function to do this?
(defn walk-leaves
"Walks a map applying a function to all non-map nodes(or leaves)"
[m f]
(let [handle-map (fn [[k v]] (if (map? v) [k v] [k (f v)]))]
(postwalk (fn [x] (if (map? x) (into {} (map handle-map x)) x)) m)))
(defn walk-leaves2
"Explicitly recursive with kitchsink/mapvals"
[m f]
(mapvals #(if (map? %) (map-leaves2 % f) (f %)) m))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment