Forked from gerritjvv/core.async-lazy-sequence
Last active
December 27, 2015 00:29
-
-
Save rodnaph/7238399 to your computer and use it in GitHub Desktop.
Not tested, but can use first, and need recur to not consume stack?
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
(use 'clojure.core.async) | |
;this is the function you want to use | |
(defn lazy-channels [chs] | |
(lazy-seq | |
(cons (first (alts!! chs)) | |
(recur chs)))) | |
;now for the tesging | |
(def chs [ (chan) (chan) (chan) ]) ; the channels can come from anywhere, here we are using three channels for testing | |
(thread (dotimes [i 1000] | |
(>!! (rand-nth chs) (str "m-" i)))) ;add 1000 elements to a random selection of channels | |
;create a sequence | |
(def s (lazy-channels chs)) | |
;now consume, please note that this will block when no more data is available on the channels | |
(doseq [msg s] | |
(prn msg)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment