Thanks James, I played around with the persistent queues before I started this exercise, but opted for atoms, after seeing this behavior (a la JOC):
<https://lh6.googleusercontent.com/-4u0XBfNvbv4/U3CMpigLRoI/AAAAAAAABjE/HDjKbcvSgAM/s1600/Screen+Shot+2014-05-12+at+5.55.27+PM.png> I never considered sticking the persistent queue in the atom and your code above made me realize that maps are a way better way to do this. Rock on, Jesse On Monday, May 12, 2014 1:16:19 PM UTC+9, James Reeves wrote: > > On 12 May 2014 03:41, gamma235 <jesus.d...@gmail.com <javascript:>> wrote: > >> >> (defn log-name [ch] (symbol (str ch '-log))) >>> >>> ;; Is this efficiently written? >>> (defmacro transparent-chan [ch] >>> (do >>> `(def ~ch (chan)) >>> `(def (log-name ~ch) (atom [])) >>> `(println "new transparent channel created!"))) >>> >> > This looks like you're replicating the functionality of maps using defs. > Instead, consider something like: > > (defn transparent-chan [ch] > {:channel ch, :buffer (atom [])}) > > Another improvement you may wish to consider is to use a queue, rather > than a vector. Immutable queues exist in Clojure, but are something of a > hidden feature. > > (defn transparent-chan [ch] > {:channel ch, :buffer (atom clojure.lang.PersistentQueue/EMPTY)}) > > Queues act more like channels, in that popping a queue strips off the > oldest item, whereas popping a vector strips off the newest. > > With this in mind, you could write functions like: > > (defn transparent-put [{:keys [channel buffer]} x] > (go > (>! channel x) > (swap! buffer conj x))) > > (defn transparent-take [{:keys [channel buffer]} x] > (go > (let [x (<! channel)] > (swap! buffer pop) > x))) > > (defn transparent-buffer [{keys [buffer]}] > (vec @buffer)) > > - James > -- 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/d/optout.