-
-
Save jamesdavidson/bedb0580d989ef94880603032f779755 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
(require '[clojure.zip :as zip]) | |
(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