Skip to content

Instantly share code, notes, and snippets.

@stathissideris
Last active October 20, 2020 19:14
Show Gist options
  • Save stathissideris/8659706 to your computer and use it in GitHub Desktop.
Save stathissideris/8659706 to your computer and use it in GitHub Desktop.
clojure async channels as lazy (and blocking!) sequences
(defn seq!!
"Returns a (blocking!) lazy sequence read from a channel."
[c]
(lazy-seq
(when-let [v (<!! c)]
(cons v (seq!! c)))))
(comment
(def stream (chan))
(go (dotimes [x 16]
(<! (timeout 500))
(>! stream x))
(close! stream))
(doseq [x (seq!! stream)] (println x)))
@aiba
Copy link

aiba commented Mar 24, 2017

Slight improvement: when-let could be a when-some to allow for streams of booleans.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment