Bleh when you see the bug as you hit "Send"....

(defn queue-process
  [input output stats]
  (async/go
    ; Wait for the first input.
    (loop [v (async/<! input)
           q clojure.lang.PersistentQueue/EMPTY]
      (if v
        (let [[val-to-q ch] (async/alts! [input [output v]])]
          (swap! stats update-stats-as-you-see-fit q)
          (cond
            ; Read a value from input.
            val-to-q (recur v (conj q val-to-q))

            ; Input channel is closed. => Empty queue.
            (identical? ch input) (recur nil (cons v q))

            ; Write happened, and there is more in the queue.
            (pos? (count q)) (recur (peek q) (pop q))

            ; Write happened, and queue is empty. Start over.
            :else (recur (async/<! input) q)))
        (do
          (doseq [v q] (async/>! output v))
          (async/close! output))))))

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to