Skip to content

Instantly share code, notes, and snippets.

@oirodolfo
Forked from luxbock/bfs.clj
Created June 4, 2017 01:14
Show Gist options
  • Select an option

  • Save oirodolfo/3d604763d8d70a23df1165d81d43807d to your computer and use it in GitHub Desktop.

Select an option

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