Created
December 29, 2014 06:32
-
-
Save luxbock/4a4cafdcf2522ede6575 to your computer and use it in GitHub Desktop.
Clojure Zipper Breadth First Search
This file contains hidden or 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 breadth-first-search [z] | |
(letfn [(zip-children [loc] | |
(when-let [first-child (zip/down loc)] | |
(take-while (comp not nil?) | |
(iterate zip/right first-child))))]) | |
(loop [ret [] | |
queue (conj clojure.lang.PersistentQueue/EMPTY z)] | |
(if (seq queue) | |
(let [[node children] ((juxt zip/node zip-children) (peek queue))] | |
(recur (conj ret node) (into (pop queue) children))) | |
ret))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment