lazy channel
This file contains 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
(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