Skip to content

Instantly share code, notes, and snippets.

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."
(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)
(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