-
-
Save schmee/f17febda78242bc15dac to your computer and use it in GitHub Desktop.
This file contains 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
; 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