Created
July 11, 2013 23:03
-
-
Save michalmarczyk/5980097 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; for now, consumer starts producer | |
;; -- must make sure that output chan already on comm-chan before producer starts | |
(defn producer [comm-chan] | |
(go (loop [] | |
(if-let [c (<! comm-chan)] | |
(do | |
(>! c (rand)) | |
(>! comm-chan c) | |
(recur)) | |
(println :done))))) | |
(defn consumer [] | |
(let [comm-chan (chan 1) | |
c (chan 5)] | |
(>!! comm-chan c) | |
(producer comm-chan) | |
(go (dotimes [i 10] | |
(println (<! c))) | |
(close! comm-chan) | |
(println (<! c))))) |
This file contains hidden or 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
user> (consumer) | |
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@39d88fef> | |
0.48858350508458315 | |
0.5446136119220218 | |
0.32582715261618633 | |
0.16814828882683197 | |
0.00485421830103927 | |
0.37070944308345277 | |
0.6165331723831634 | |
0.28219642381915655 | |
0.5014768991693602 | |
0.9350411298635354 | |
0.6912907965087149 | |
:done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment