Created
November 19, 2015 08:57
-
-
Save friemen/d095d6ae8b872c067e95 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
;; 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