@Meikel, I like your solution. Its key advantage is that you have enable arbitrary functionality in the "monitoring" of a channel. Since I am new to Clojure, and I bet others are too, I was subconsciously thinking about creating a new kind of channel (monitored channel). This solution does something similar, by defining a new output channel that is a copy of the previous channel while enabling monitoring. You've done this without a new type of class.
I particularly like this better than simply adding "count" because it solves the problem of *when* to call count for monitoring. Your code calls count iff count changes. Regardless of implementation, a monitored channel is a good thing. Not all channels have zero queue. Thanks, Paul On Friday, January 17, 2014 2:54:47 AM UTC-8, t x wrote: > > @Meikel: I am now convinced that you are right. It's clear to me that I > completely underestimated the power / flexibility of the Channel > abastraction. > > @World: I now retract my second-ing of "adding count to Protocol of > Channel" > > > On Fri, Jan 17, 2014 at 2:10 AM, Meikel Brandmeyer (kotarak) < > m...@kotka.de <javascript:>> wrote: > >> Hi again, >> >> and some more golfing by Christophe: >> >> (defn queue-process-uncontrolled >> [input output stats] >> (async/go >> (loop [q clojure.lang.PersistentQueue/EMPTY] >> (let [[val-to-q ch] (async/alts! >> (if-let [v (peek q)] >> [input [output v]] >> [input]))] >> (swap! stats update-stats-as-you-see-fit q) >> (cond >> ; Read a value from input. >> val-to-q (recur (conj q val-to-q)) >> ; Input channel is closed. => drain queue. >> (identical? ch input) (doseq [v q] (async/>! output v)) >> >> ; Write happened. >> :else (recur (pop q))))) >> >> (defn queue-process-controlled >> [input stats] >> (let [output (async/chan) >> process (queue-process-uncontrolled input output stats)] >> (async/go >> (<! process) >> (async/close! output)) >> output)) >> >> >> Plus an improvement for the closing of the output channel. >> queue-process-uncontrolled is not necessarily the master of the channel. >> >> Meikel >> >> -- >> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clo...@googlegroups.com<javascript:> >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+u...@googlegroups.com <javascript:> >> 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+u...@googlegroups.com <javascript:>. >> For more options, visit https://groups.google.com/groups/opt_out. >> > > -- -- 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.