beginners

2026-05-11T13:28:18.361649Z

how does eduction work with stateful transducers? for example, the below prints [0 1 2] three times. wouldn't take be "used up" the first time? when is the xform "copied"/duplicated?

(let [e (eduction (take 3) (range 10))]
  (prn (vec e))
  (prn (vec e))
  (prn (vec e)))

✅ 1
ghadi 2026-05-11T13:36:08.538679Z

a transducing context must transform its internal 'step/proc' fresh. can't use a possibly already transformed step/proc because it would behave unpredictably. eduction uses transduce, which freshly transform the proc every time:

...
  ([xform f init coll]
     (let [f (xform f) ;; in your example ((take 3) conj)

2026-05-11T13:36:48.786859Z

ah there it is, thank you. i forgot about that part of the transducer flow

2026-05-11T13:40:00.140639Z

i forgot that transducers are HOFs closing over the reducing function, so the ones that take inputs (like take) are 2x HOFs, closing over first the arg, and then over the reducing function, returning the "actual" transducer at the bottom

2026-05-11T13:40:28.043779Z

complicated heh

ghadi 2026-05-11T13:40:34.179849Z

also why transducers that are stateful must create their state inside the application of a single transformation, not prior

👍 1
ghadi 2026-05-11T13:40:36.301849Z

jinx

😄 1