Skip to content

Instantly share code, notes, and snippets.

@friemen
Created November 19, 2015 08:57
Show Gist options
  • Save friemen/d095d6ae8b872c067e95 to your computer and use it in GitHub Desktop.
Save friemen/d095d6ae8b872c067e95 to your computer and use it in GitHub Desktop.
;; walk tree-ish vector with Zippers in recursive fashion
(defn walktree
[t]
(letfn [(inner [loc numbers]
(let [node (z/node loc)
numbers (conj numbers (if (sequential? node) (first node) node))
loc (z/edit loc #(if (number? %)
(inc %)
(into [(inc (first %))] (rest %))))]
(println numbers)
(if (z/branch? loc)
(loop [child (z/down loc)]
(if-let [next-child (z/right child)]
(recur (inner next-child numbers))
(z/up child)))
loc)))]
(-> t (z/vector-zip) (inner []) (z/root))))
(def v [1 [2 3] 4 [5 6]])
(walktree v)
;; [1]
;; [1 2]
;; [1 2 3]
;; [1 4]
;; [1 5]
;; [1 5 6]
;;=> [2 [3 4] 5 [6 7]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment