Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
lazy channel
(require '[clojure.core.async :as a])
(defn pull-seq
"Returns a (blocking!) lazy sequence read from a channel."
[c]
(lazy-seq
(a/>!! c :ready)
(when-let [v (a/<!! c)]
(cons v (pull-seq c)))))
;; this is the producer
(defn produce [c]
(a/go-loop [v 0]
(when (= :ready (a/<! c))
(println "channel is ready for more, working... ")
;; emulate work
(a/<! (a/timeout (rand-int 100)))
;; browser
(println "putting value " v)
;; send back a value and wait for more
(a/>! c v)
(recur (inc v)))))
(def c (a/chan))
(produce c)
(a/thread
(doall (pmap println (pull-seq c))))
(a/close! c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment