Created
November 1, 2020 13:11
-
-
Save viebel/93b8905abe1e1778cd0c39ae73992572 to your computer and use it in GitHub Desktop.
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 [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