Skip to content

Instantly share code, notes, and snippets.

@vbedegi
Forked from gerritjvv/core.async-lazy-sequence
Created November 4, 2013 08:12
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 vbedegi/7299564 to your computer and use it in GitHub Desktop.
Save vbedegi/7299564 to your computer and use it in GitHub Desktop.
(use 'clojure.core.async)
;this is the function you want to use
(defn lazy-channels [chs]
(lazy-seq (cons (let [ [v _] (alts!! chs)] v) (lazy-channels 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