web articles I've read about transducer
Js
- Transducers: Efficient Data Processing Pipelines in JavaScript @ Eric Elliott -Medium
- Understanding Transducers in JavaScript @ Roman Liutikov-Medium
hard to understand from half...
- What's a Transducer?
- Simpler Transducers for JavaScript
- How to make your data transformations more efficient using transducers
Clojure
- TRANSDUCERS ARE COMING by Rich Hickey-cognitect
- Transducers-Clojure.org
I read Clojure official tutorial about 2 pages, and understood the basic syntax. I refered to built-in function reference to understand the transducer example code.
my understanding about above two article is probably 75%...
my Question
I want to know whether the following understanding/js code is correct or incorrect. Please help me.<(_ _)>
About transducer
- The returned value by
compose()
is a transducer. - Transducer is executed by passed to
transduce()
function as argument and, in addition, (2)Transducer is executed by passing array directly totransducer()
. In (2)'s process, intermediate value isn't generated and efficient process like below link is performed.
my code
"use strict";
const map = fn => arr => arr.map(fn),
filter = fn => arr => arr.filter(fn),
addReducer = arr => arr.reduce((acc, num) => acc + num, 0),
add1 = n => n + 1,
even = n => n % 2 === 0,
compose = (...fns) => initVal => fns.reduce((acc, fn) => fn(acc), initVal),
transduce = (xform, reducer, arr ) => reducer( xform(arr) );
const arr = [1,2,3],
transducer = compose( /* called transducer or xform */
map( add1 ), // 2,3,4
filter( even ), // 2,4
);
console.log( transducer(arr) ) // 2,4
console.log( transduce(transducer, addReducer, arr) ) // 6
compose
is wrong. WHen you passcompose(a, b)
it turns intob(a(initVal))
while it should be applied in reverse ordera(b(initVal))
– Sylwesterpipe
. – Sylwester