Created
September 1, 2012 14:00
-
-
Save bnyeggen/3573955 to your computer and use it in GitHub Desktop.
Blocking queues to lazy seqs and vice versa
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
(def ^:private poison (Object.)) | |
(defn seq-to-qs | |
"Place each element of the lazy seq in each of the queues, and finally the | |
poison value." | |
[s & qs] | |
(doseq [e s q qs] (.put ^BlockingQueue q e)) | |
(doseq [q qs] (.put ^BlockingQueue q poison))) | |
(defn lazy-q-seq | |
"Take a BlockingQueue and return a lazy sequence of its values. Stop when we | |
hit the poison value." | |
[^BlockingQueue q] | |
(let [this (.take q)] | |
(if-not (identical? this poison) | |
(lazy-seq (cons this (lazy-q-seq q)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment