On 12 May 2014 03:41, gamma235 <[email protected]> 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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.