Transducers for JavaScript

  • When I see concepts like transducers I'm always torn in two:

    my theoretical side 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.

    my pragmatic side is skeptic: in functional programming it's a little step to end up with incomprehensible code, for me and my coworkers:

        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]
    
    vs

        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]
    
    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:

        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
        }

  • From: http://phuu.net/2014/08/31/csp-and-transducers.html

    "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: https://github.com/jlongster/transducers.js, 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.

  • undefined

  • 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

        var f=_.compose( _.partialRight(_.filter,isEven),_.partialRight(_.map,inc)) ;
    
    or something.

  • As somebody with more experience with Haskell and Scala than Clojure - how to Transducers differ from Functors in Haskell?

    On the surface they seem very similar.

  • Friendly advice: maybe also include the output of the example code in the README.

  • Notes and examples of Transducers and Reducers https://gist.github.com/runexec/06b56a9dbd15e43145b9

  • Why not wait for ES6, and use proper iterators and use them as they do in C#?

    as in var evens = [1,2,3,4].where(x => x%2 == 0).toarray()

    no ?