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