What is the reasoning behind the Transducers eduction function?

529 views
Skip to first unread message

Jason Gilman

unread,
Oct 7, 2014, 8:12:27 PM10/7/14
to clo...@googlegroups.com
There's a few new functions that have been added with Transducers which mostly have very clear use cases to me. What are the use cases for the new eduction function? (which looks like it was previously called iteration). The example at http://clojure.org/transducers is informative. It looks similar to sequence in that it produces something that is seq-able with the major difference being that the transform is applied each time the sequence is traversed.

You can see the difference here in this code (This uses iteration instead of eduction because I was on 1.7.0-alpha2)

(def to-str-iteration 
  (iteration (map (fn [i]
                    (println "iteration" i)
                    (str i)))
             (range 3)))

(def to-str-seq 
  (sequence (map (fn [i]
                   (println "sequence" i)
                   (str i)))
            (range 3)))

(println "First iteration")
(dorun to-str-iteration)
(println "Second iteration")
(dorun to-str-iteration)
(println "First sequence")
(dorun to-str-seq)
(println "Second sequence")
(dorun to-str-seq)

Printed:
First iteration
iteration 0
iteration 0
iteration 1
iteration 2
Second iteration
iteration 0
iteration 0
iteration 1
iteration 2
First sequence
sequence 0
sequence 1
sequence 2
Second sequence

Should we use eduction when we need side effects or reading the current state during a transform and sequence when the transform is pure? Or is there a subtler difference between the two that I'm missing?

Laurent PETIT

unread,
Oct 8, 2014, 1:25:38 AM10/8/14
to clo...@googlegroups.com
Hello,

eduction does not return a seq, or anything already "concrete" like that, rather it returns a new reducible, with the interesting property lying in the retention of the xform argument (a transducer). 

This allows you, for instance, to pass around a collection with all the processing steps but the final rendering of the result (a seq? an aggregate result? a vector ? nil?) => it's up to the user of the educible. 

Since the educible is an unreduced form of the initial collection, then every time it will be "realized" into a concrete value, the hidden (behind the xform) collection will be traversed, and the transducer-returned reducing function called. 
--
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+u...@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 the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Laurent Petit

Reply all
Reply to author
Forward
0 new messages