-
-
Save oirodolfo/3d604763d8d70a23df1165d81d43807d 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