Last active
December 19, 2015 21:19
-
-
Save martintrojer/6019215 to your computer and use it in GitHub Desktop.
core.async non-tailrecursive
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
(defn walk [tree ch] | |
(letfn [(walker [t] | |
(go | |
(when t | |
(walker (:left t)) | |
(>! ch (:value t)) | |
(walker (:right t)))))] | |
(walker tree) | |
(close! ch))) |
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
(defn walk [tree ch] | |
(letfn [(walker [t] | |
(go | |
(when t | |
(walker (:left t)) | |
(>! ch (:value t)) | |
(walker (:right t)))))] | |
(go | |
(<! (walker tree)) | |
(close! ch)))) |
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
(defn walk [tree ch] | |
(letfn [(walker [t] | |
(go | |
(when t | |
(<! (walker (:left t))) | |
(>! ch (:value t)) | |
(<! (walker (:right t))))))] | |
(go | |
(<! (walker tree)) | |
(close! ch)))) |
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
(defn walk [tree ch] | |
(go | |
((fn walker [t] | |
(when t | |
(walker (:left t)) | |
(>! ch (:value t)) | |
(walker (:right t)))) | |
tree) | |
(close! ch))) |
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
(defn walk [tree ch] | |
(letfn [(walker [t] | |
(when t | |
(walker (:left t)) | |
(>!! ch (:value t)) | |
(walker (:right t))))] | |
(walker tree) | |
(close! ch))) |
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
(defn put-all! [vs ch] | |
(doseq [v vs] (>! ch v))) | |
(let [ch (chan)] | |
(go (put-all! [42 84] ch)) | |
[(<!! ch) (<!! ch)]) |
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
(let [ch (chan)] | |
(go ((fn [vs] (doseq [v vs] (>! ch v))) | |
[42 84])) | |
[(<!! ch) (<!! ch)]) |
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
(let [ch (chan)] | |
(go (doseq [v [42 84]] (>! ch v))) | |
[(<!! ch) (<!! ch)]) |
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
(let [ch (chan)] | |
(go (loop [[f & r] [42 84]] ;; equivalent to (doseq) | |
(when f (>! ch f) (recur r)))) | |
[(<!! ch) (<!! ch)]) |
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
(defn same [t1 t2] | |
(let [ch1 (chan) | |
ch2 (chan) | |
drain #(loop [v (<!! %) res []] | |
(if v (recur (<!! %) (conj res v)) res))] | |
(thread (walk t1 ch1)) | |
(thread (walk t2 ch2)) | |
(= (drain ch1) (drain ch2)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment