When I try this using your code above I get a stack-trace that I can't understand. Am I using it wrong?
(transparent-chan c) > (transparent-put c 42) > (transparent-take c) - > > "Exception in thread \"async-dispatch-64\" " > "java.lang.IllegalArgumentException: No implementation of method: > :take! of protocol: #'clojure.core.async.impl.protocols/ReadPort found for > class: nil" > "\tat clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:541)" 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.