Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Process a zipper structure
(defn render-element [open-or-close node]
(let [slash (if (= open-or-close :open)
""
"/")]
(if (map? node)
(str "<" slash (name (:tag node)) ">")
node)))
(defn traverse [root arrive-fn leave-fn]
(loop [loc root
nodes []
direction :arrive]
(let [node-fn (if (= direction :arrive) arrive-fn leave-fn)
new-nodes (conj nodes (node-fn (zip/node loc)))]
(if (and (= loc root) (= direction :leave))
new-nodes
(let [child-loc (zip/down loc)
right-loc (zip/right loc)]
(if (or (nil? child-loc) (= direction :leave))
(if (nil? right-loc)
(recur (zip/up loc) new-nodes :leave)
(recur right-loc new-nodes :arrive))
(recur child-loc new-nodes :arrive)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.