0-arity of transducer in transduce

141 views
Skip to first unread message

Mathias De Wachter

unread,
Aug 24, 2016, 4:00:41 AM8/24/16
to Clojure
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

Bobby Eickhoff

unread,
Aug 31, 2016, 9:58:48 PM8/31/16
to Clojure
Mathias, I've never found a satisfactory answer to this question.  It has been asked before.


Hoping someone with more insight will comment on this situation.

Bobby

Mathias De Wachter

unread,
Sep 1, 2016, 1:53:44 AM9/1/16
to clo...@googlegroups.com
Thanks Bobby! And apologies for being too lazy to search for these threads.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+unsubscribe@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 a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/2VBF11AHe58/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages