Skip to content

Instantly share code, notes, and snippets.

@bnyeggen
Created September 1, 2012 14:00
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 bnyeggen/3573955 to your computer and use it in GitHub Desktop.
Save bnyeggen/3573955 to your computer and use it in GitHub Desktop.
Blocking queues to lazy seqs and vice versa
(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