Skip to content

Instantly share code, notes, and snippets.

@luxbock
Created December 29, 2014 06:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save luxbock/4a4cafdcf2522ede6575 to your computer and use it in GitHub Desktop.
Save luxbock/4a4cafdcf2522ede6575 to your computer and use it in GitHub Desktop.
Clojure Zipper Breadth First Search
(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