What about the following? (as is bound to clojure.core.async ns)
(defn decorate-channel
[chan at]
(as/map> (fn [x] (swap! at conj x) x)
(as/map< (fn [x] (swap! at pop) x)
chan)))
As far as I can tell, it should do what you want, without introducing
global vars. It also returns a "normal" async channel that can be used
anywhere a channel is expected, without special handling. This could
be very handy for debugging.
On 13 May 2014 06:02, gamma235 <[email protected]> wrote:
>
> Hey guys, I took your suggestions and just wanna deliver the finished
> product. I included the macro in there for practice, but agree it is
> probably not necessary. Thanks for all your help.
>
> p is for pretty :)
>
>>
>> (defn pchan [ch]
>>
>> {:channel ch, :buffer (atom [])})
>
>
>>
>> (defn pbuff [{:keys [ch buffer]}]
>> (println @buffer))
>
>
>>
>> (defn pput [{:keys [channel buffer]} v]
>> (do
>> (go
>> (>! channel v))
>> (swap! buffer conj v)
>> (println "put " v)))
>
>
>>
>> (defn ptake [{:keys [channel buffer]}]
>> (go
>> (let [v (<! channel)]
>> (if-not (empty? @buffer)
>> (do
>> (swap! buffer pop)
>> (println "took " v)))))
>> (println "take pending ..."))
>
>
>>
>> (defmacro def-pchan
>> ([name]
>> `(def ~name (pchan (chan))))
>> ([name buff]
>> `(def ~name (pchan (chan ~buff)))))
>
>
>>
>> (defn clear-buffer [{:keys [buffer]}]
>> (reset! buffer []))
>>
> it works!
>>
>> (def-pchan pc)
>> (:buffer pc)
>> (pbuff pc)
>> (ptake pc)
>> (pput pc 42)
>> (clear-buffer pc)
>
> --
> 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.
--
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.