Ramda: Quick question on transduce

Created on 3 Jun 2016  Â·  1Comment  Â·  Source: ramda/ramda

Can anyone explain why when I run ...

const logAndId = (n) => (x) => { console.log(n); return x }
const xform = R.pipe(R.map(logAndId(1)), R.map(logAndId(2)), R.map(logAndId(3)))
R.transduce(xform, R.flip(R.append), [], [5])

... I get 3, 2 & 1 in the logs?

If I change to compose I get 1, 2, & 3.

Most helpful comment

this might shed some light on the subject: http://isaaccambron.com/blog/2014/12/13/transducer-composition.html

So why does composing transducers mean they get evaluated “backwards” or “inside out”? Well, on reflection, it makes a lot of sense. What transducers really do is transform reducing functions, not actual values; they take one reducing function and return another one that works by transforming its values and passing the results to that passed-in reducing function. When you compose them, you’re using the function returned by the “inner” transducer as the reduction function for the “outer” transducer. So if I have (comp (map double) (map inc)), I’m saying that (map inc) provides a reducing function that takes a value, increments it, and feeds into the reducing function it gets passed. I’m then passing that reducing function into the doubling tansducer, which returns another reducing function that doubles the values and then feeds the answer into the map-incrementing reducer the doubler took as an argument. So double then inc.

>All comments

this might shed some light on the subject: http://isaaccambron.com/blog/2014/12/13/transducer-composition.html

So why does composing transducers mean they get evaluated “backwards” or “inside out”? Well, on reflection, it makes a lot of sense. What transducers really do is transform reducing functions, not actual values; they take one reducing function and return another one that works by transforming its values and passing the results to that passed-in reducing function. When you compose them, you’re using the function returned by the “inner” transducer as the reduction function for the “outer” transducer. So if I have (comp (map double) (map inc)), I’m saying that (map inc) provides a reducing function that takes a value, increments it, and feeds into the reducing function it gets passed. I’m then passing that reducing function into the doubling tansducer, which returns another reducing function that doubles the values and then feeds the answer into the map-incrementing reducer the doubler took as an argument. So double then inc.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielFGray picture DanielFGray  Â·  3Comments

kostenko picture kostenko  Â·  3Comments

kanitsharma picture kanitsharma  Â·  3Comments

MadDeveloper picture MadDeveloper  Â·  3Comments

ldk18501 picture ldk18501  Â·  3Comments