Hi,
I'm trying to use the transducer framework for some sequence processing where I have a core reduction function, but depending on options, extra functionality can be added to the reduction function. I thought that's a good fit for hand-written transducers.
However, I'm running into a problem initializing the optional functionality. I thought I would simply perform it in the 0-arity, like this:
(defn my-tx
[rf]
(fn
([] (assoc (rf) ::my-ns/my-extra-field my-init-value)
...
But that's never called, at least not in transduce:
(defn transduce
"..." {:added "1.7"}
([xform f coll] (transduce xform f (f) coll))
([xform f init coll]
(let [f (xform f)
ret (if (instance? clojure.lang.IReduceInit coll)
(.reduce ^clojure.lang.IReduceInit coll f init)
(clojure.core.protocols/coll-reduce coll f init))]
(f ret))))So, my question is: why the (f) instead of (xform f)? And what's the use of the 0-arity in transducers if it's not called? I assume it's used in other uses of transducers?
By the way, I understand it's tricky to call (xform f) instead, because then what do you do with a provided init... And I also understand that I can create initial state in the closure of the transducer, so I'm not blocked at all. I'm just very curious :).
Thanks,
-Mathias