Skip to content

Instantly share code, notes, and snippets.

@viebel
Created November 1, 2020 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viebel/93b8905abe1e1778cd0c39ae73992572 to your computer and use it in GitHub Desktop.
Save viebel/93b8905abe1e1778cd0c39ae73992572 to your computer and use it in GitHub Desktop.
(require '[clojure.walk :refer [prewalk postwalk]])
(def data {:a 1
:b {:c 2}
:d [3 {:e 4}]})
;; prewalk: node -> map f to children of f(node)
;; prewalk: [a b c] -> (map f (f [a b c]))
;; prewalk: transform node and then transform chilren
;; postwalk: node -> call f on map f to children of node
;; postwalk: [a b c] -> (f (map f [a b c]))
;; postwalk: transform children and the transform node
(postwalk (fn [x]
(if (coll? x)
{"_type" (type x)
"_value" x}
x))
data) ;;; with prewalk, it caused an infinite recursion
(postwalk (fn [x]
(if (coll? x)
(into [:added] x)
(str x)))
[1 2 3]) ;; :added is not transformed
(prewalk (fn [x]
(if (coll? x)
(into [:added] x)
(str x)))
[1 2 3]) ;; :added is transformed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment