iteration - How to iterate over a clojure eduction - without creating a seq?


Keywords:clojure 


Question: 

For the sake of this question, let's assume I created the following eduction.

(def xform (map inc))
(def input [1 2 3])
(def educt (eduction xform input))

Now I want to pass educt to some function that can then do some kind of reduction. The reason I want to pass educt, rather than xform and input is that I don't want to expose xform and input to that function. If I did, that function could simply do a (transduce xform f init input). But as I don't, that function is left with an eduction that cannot be used with transduce.

I know I can e.g. use doseq on eductions, but I believe this will create a seq - with all its overhead in terms of object instantiation and usage for caching.

So how can I efficiently and idiomatically iterate over an eduction?

As eductions implement java.lang.Iterable, this question probably generalizes to:

How to iterate over a java.lang.Iterable without creating a seq?


1 Answer: 

reduce can be used to do that.

It works on instances of IReduceInit, which eduction implements.