This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-07-10
Channels
- # admin-announcements (4)
- # beginners (21)
- # boot (85)
- # cider (31)
- # clojure (45)
- # clojure-australia (1)
- # clojure-berlin (2)
- # clojure-dev (7)
- # clojure-italy (10)
- # clojure-japan (17)
- # clojure-norway (8)
- # clojure-russia (2)
- # clojure-uk (15)
- # clojurebridge (1)
- # clojurescript (115)
- # code-reviews (13)
- # core-async (1)
- # editors (17)
- # euroclojure (5)
- # jobs (1)
- # ldnclj (9)
- # off-topic (5)
- # om (18)
- # re-frame (15)
- # reagent (25)
- # sydney (1)
Hey all, I'm trying to write my first transducer. I'm processing an endless channel of data and would like to simple do something every n elements processed. Does this look right?
(defn do-every
"Returns a transducer that executes f every n steps."
[n f]
(fn [rf]
(let [i (volatile! 0)]
(fn
([] (rf))
([result] (rf result))
([result input]
(when (zero? (mod (vswap! i inc) n))
(f))
(rf result input))))))
I copied the volatile!
from an example on http://clojure.org/transducers, but, I admit, I'm not entirely sure what it does...looks reasonable. I think of volatile as an atom that can only be written to from a single thread at a time, but is faster than an atom because of that. If you find good docs explaining its usage, please share!
devon: note that channel transducers run inside the internal channel locks, so that should be a quick side-effect
This one is, it's just logging. But, out of curiosity, is it any worse to have a potentially blocking code in transducer on the channel vs something that's consuming off of it? Assuming that it's only one thread is consuming?