Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2012 12:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4327681 to your computer and use it in GitHub Desktop.
Save anonymous/4327681 to your computer and use it in GitHub Desktop.
(defn pipe
"Returns a vector containing a sequence that will read from the
queue, and a function that inserts items into the queue.
Source: http://clj-me.cgrand.net/2010/04/02/pipe-dreams-are-not-necessarily-made-of-promises/"
[]
(let [q (LinkedBlockingQueue.)
EOQ (Object.)
NIL (Object.)
s (fn queue-seq []
(lazy-seq (let [x (.take q)]
(when-not (= EOQ x)
(cons (when-not (= NIL x) x)
(queue-seq))))))]
[(s) (fn queue-put
([] (.put q EOQ))
([x] (.put q (or x NIL))))]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment