When I see concepts like transducers I'm always torn in two:<p><i></i>my theoretical side<i></i> is really excited: reducing (pun intended) a bunch of different operations to a minimal set of basic, composable operations is an awesome intellectual challenge. I imagine how succinct, reusable and multipurpose could become my code.<p><i></i>my pragmatic side<i></i> is skeptic: in functional programming it's a little step to end up with incomprehensible code, for me and my coworkers:<p><pre><code> var inc = function(n) { return n + 1; };
var isEven = function(n) { return n % 2 == 0; };
var xf = comp(map(inc), filter(isEven));
console.log(into([], xf, [0,1,2,3,4])); // [2,4]
</code></pre>
vs<p><pre><code> var inc = function(n) { return n + 1; };
var isEven = function(n) { return n % 2 == 0; };
[0,1,2,3,4].map(inc).filter(isEven); // [2,4]
</code></pre>
The latter is comprehensible by everyone (and no additional library).
Ok you iterate twice but if you really (REALLY) have performance problems, maybe you'd end up with something like this:<p><pre><code> var input = [0,1,2,3,4];
var output = [];
var x;
for (var i = 0, len = input.length ; i < len ; i++ ) {
x = input[i] + 1; // map
if (x % 2 == 0) ouput.push(x); // filter
}</code></pre>
From: <a href="http://phuu.net/2014/08/31/csp-and-transducers.html" rel="nofollow">http://phuu.net/2014/08/31/csp-and-transducers.html</a><p>"To me, transducers are a generic and composable way to operate on a collection of values, producing a new value or new collection of new values. The word 'transducer' itself can be split into two parts that reflect this definition: 'transform' — to produce some value from another — and 'reducer' — to combine the values of a data structure to produce a new one."
It'll be interesting to look into how the Clojure people implemented this. I released the same library a few weeks ago: <a href="https://github.com/jlongster/transducers.js" rel="nofollow">https://github.com/jlongster/transducers.js</a>, and I'm about to release the next version with an API almost the same as the OP. I think mine has more integration with JS data structures and stuff like monkeypatching existing ones (like immutable-js) to work with this. We'll see how all this shakes out though; not sure if it'll be good to converge on one official library or if it's ok to have multiple.
So I checked the definition of a transducer,but i'm not smart enough to get it. What the difference between that library and say lodash where I can bind and compose functions? because it seems like the same stuff<p><pre><code> var f=_.compose( _.partialRight(_.filter,isEven),_.partialRight(_.map,inc)) ;
</code></pre>
or something.
As somebody with more experience with Haskell and Scala than Clojure - how to Transducers differ from Functors in Haskell?<p>On the surface they seem very similar.
Notes and examples of Transducers and Reducers
<a href="https://gist.github.com/runexec/06b56a9dbd15e43145b9" rel="nofollow">https://gist.github.com/runexec/06b56a9dbd15e43145b9</a>