Skip to content

Instantly share code, notes, and snippets.

@mping
Created November 27, 2020 13:15
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 mping/f875954d18670de0600273bddd02c865 to your computer and use it in GitHub Desktop.
Save mping/f875954d18670de0600273bddd02c865 to your computer and use it in GitHub Desktop.
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