Playing around with transducers, what about the zero-arity of the reducing function

197 views
Skip to first unread message

Gijs S.

unread,
Sep 16, 2014, 12:56:18 PM9/16/14
to clo...@googlegroups.com
Hi,

At a recent Amsterdam Clojure Meetup, we were looking into transducers.

We could not find any situation where the "zero arity" println would get called when using this futile transducer:

(defn println-transducing-fn
  [reducing-fn]
  (fn new-reducing-fn
    ([]
       (println "zero arity")
       (reducing-fn))
    ([accumulation]
       (println "one arity")
       (reducing-fn accumulation))
    ([accumulation new-input]
       (println "two arity")
       (reducing-fn accumulation new-input))))

So, out of curiosity; what would be an example (by using transduce / sequence / iteration / core.async/chan etc) that would print "zero arity"?

-Gijs

Ghadi Shayban

unread,
Sep 16, 2014, 1:48:51 PM9/16/14
to clo...@googlegroups.com
Call 'transduce without an init value, and it will use the 0-arity return as init. Same for reduce but in different scenarios (some IReduce/CollReduce collections)

Gijs S.

unread,
Sep 16, 2014, 2:11:38 PM9/16/14
to clo...@googlegroups.com
No init value given with transduce uses the zero arity value of f rather than the zero arity of the reducing function through xform:

With Clojure 1.7.0-alpha2:
user=> (doc transduce)
-------------------------
clojure.core/transduce
([xform f coll] [xform f init coll])
  reduce with a transformation of f (xf). If init is not
  supplied, (f) will be called to produce it.
user=> (transduce println-transducing-fn + [1 2])
two arity
two arity
one arity
3
user=> (transduce println-transducing-fn + [])
one arity
0

Ghadi Shayban

unread,
Sep 16, 2014, 2:39:26 PM9/16/14
to clo...@googlegroups.com
I was sloppy with details. A reducing function like f may be transformed though by a transducer (a priori), and a transducer must preserve the contract.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages