Skip to content

Instantly share code, notes, and snippets.

@mamboking
Created September 12, 2011 19:56
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 mamboking/1212217 to your computer and use it in GitHub Desktop.
Save mamboking/1212217 to your computer and use it in GitHub Desktop.
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