Last active
October 20, 2020 19:14
-
-
Save stathissideris/8659706 to your computer and use it in GitHub Desktop.
clojure async channels as lazy (and blocking!) sequences
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
(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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Slight improvement:
when-let
could be awhen-some
to allow for streams of booleans.