Skip to content

Instantly share code, notes, and snippets.

@schmee
Created August 7, 2014 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmee/f17febda78242bc15dac to your computer and use it in GitHub Desktop.
Save schmee/f17febda78242bc15dac to your computer and use it in GitHub Desktop.
; This works as intended
(defn bfs [from]
(letfn [(bfs-inner [frontier seen]
(lazy-seq
(println frontier seen)
(let [artist-and-similar (first frontier)
similar (similar-with-parent (second artist-and-similar))
new-frontier (concat (rest frontier) (remove #(contains? seen (second %)) similar))
new-seen (clojure.set/union seen (set (flatten similar)))]
(cons artist-and-similar (bfs-inner new-frontier new-seen)))))]
(bfs-inner [(list nil from)] #{})))
; This doesn't
(defn bfs2 [from]
(letfn [(bfs-inner [frontier seen]
(println frontier seen)
(let [artist-and-similar (first frontier)
similar (similar-with-parent (second artist-and-similar))
new-frontier (concat (rest frontier) (remove #(contains? seen (second %)) similar))
new-seen (clojure.set/union seen (set (flatten similar)))]
(cons artist-and-similar (bfs-inner new-frontier new-seen))))]
(lazy-seq (bfs-inner [(list nil from)] #{}))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment