Transducer

C4ce16f549c450f4759eb37f5d5d1a63?s=47 othree
October 03, 2015

 Transducer

Explain transducer in JS code snippets

C4ce16f549c450f4759eb37f5d5d1a63?s=128

othree

October 03, 2015
Tweet

Transcript

  1. 9.

    z = f(y) y = g ( x ) z

    = f ( g ( x ))
  2. 39.

    function even (input) { return (input % 2 == 0);

    } [1,2,3,4].filter(even); // [2, 4]
  3. 45.

    [1,2,3,4].reduce(function (result, input) { return ( input % 2 ==

    0 ? concat(result, input) : result ); }, []) // => [2, 4]
  4. 54.

    map reducing function map transducer filter reducing function filter transducer

    take reducing function take transducer replace reducing function replace transducer
  5. 55.

    var c = chan( 1, // buffer size compose( //

    Only events with even x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) );
  6. 56.

    var c = chan( 1, // buffer size compose( //

    Only events with page x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) );
  7. 57.

    var c = chan( 1, // buffer size compose( //

    Only events with page x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) );
  8. 58.

    var c = chan( 1, // buffer size compose( //

    Only events with page x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) );
  9. 59.

    var c = chan( 1, // buffer size compose( //

    Only events with page x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) ); transducer transducer
  10. 60.

    var c = chan( 1, // buffer size compose( //

    Only events with page x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) ); filter transducer constructor map transducer constructor
  11. 61.

    var c = chan( 1, // buffer size compose( //

    Only events with page x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) ); filter predicate map transform
  12. 64.

    var c = chan( 1, // buffer size compose( //

    Only events with page x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) ); f
  13. 65.

    var c = chan( 1, // buffer size compose( //

    Only events with even x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) ); g
  14. 67.

    var c = chan( 1, // buffer size // Only

    events with even x & y // And transform format to [type, x, y] compose( // Only events with even x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) ); h
  15. 69.

    x g ! y f ! z coll 0 =

    reduce ( z, coll )
  16. 70.
  17. 71.
  18. 79.

    var receipt = compose( mapping(incOne), filtering(even) ); var reduceToArray =

    function (result, input) { return result.push(input); } [1,2,3,4].reduce( receipt(reduceToArray) , []); // [2, 4]
  19. 80.

    var receipt = compose( mapping(incOne), filtering(even) ); var reduceToArray =

    function (result, input) { return result.push(input); } [1,2,3,4].reduce( receipt(reduceToArray) , []); // [2, 4] x h z h ( x )
  20. 81.

    var receipt = compose( mapping(incOne), filtering(even) ); var reduceToArray =

    function (result, input) { return result.push(input); } [1, 2, 3, 4].reduce( receipt(reduceToArray) , []); // [2, 4] CollReduce initial value iterate
  21. 84.

    +RZ

  22. 90.

    ,QF2QH7UDQVGXFHU function (reducing) { return function (result, input) { return

    reducing(result, input + 1); }; } map transform CollReduce
  23. 92.

    0DS7UDQVGXFHU&RQVWUXFWRU function mapping(transform) { return function (reducing) { return function

    (result, input) { return reducing(result, transform(input)); }; }; }
  24. 93.

    function mapping(transform) { return function (reducing) { return function (result,

    input) { return reducing(result, transform(input)); }; }; } transducer constructor
  25. 94.

    function mapping(transform) { return function (reducing) { return function (result,

    input) { return reducing(result, transform(input)); }; }; } transducer x g ! y f ! z g
  26. 95.

    function mapping(transform) { return function (reducing) { return function (result,

    input) { return reducing(result, transform(input)); }; }; } reducing function
  27. 96.

    function mapping(transform) { return function (reducing) { return function (result,

    input) { return reducing(result, transform(input)); }; }; } whatever
  28. 97.

    function mapping(transform) { return function (reducing) { return function (result,

    input) { return reducing(result, transform(input)); }; }; } CollReduce initial value map transform x y x g ! y f ! z x g ! y f ! z
  29. 98.

    )LOWHU7UDQVGXFHU&RQVWUXFWRU function filtering(predicate) { return function (reducing) { return function

    (result, input) { return ( predicate(input) ? reducing(result, input) : result ); }; }; }
  30. 99.

    7DNH7UDQVGXFHU&RQVWUXFWRU function taking(n) { return function (reducing) { return function

    (result, input) { if (n > 0) { n--; return reducing(result, input); } else { return result; } }; }; }
  31. 100.

    'URS7UDQVGXFHU&RQVWUXFWRU function dropping(n) { return function (reducing) { return function

    (result, input) { if (n > 0) { n--; return result; } else { return reducing(result, input); } }; }; }
  32. 101.

    var c = chan( 1, // buffer size compose( //

    Only events with even x & y filtering(function (e) { return ( even(e.pageX) && even(e.pageY) ); }), // e -> [type, x, y] mapping(function (e) { return [e.type, e.pageX, e.pageY]; }) ) );
  33. 105.

    var map = t.map, filter = t.filter, comp = t.comp,

    into = t.into, apush = function(arr, x) { arr.push(x); return arr; }, var inc = function(n) { return n + 1; }; var isEven = function(n) { return n % 2 == 0; }; var xf = comp(map(inc), filter(isEven)); into([], xf, [0,1,2,3,4]); // [2,4] transduce(xf, apush, 0, [0,1,2,3,4]); // [2,4]
  34. 106.

    var map = t.map, filter = t.filter, comp = t.comp,

    into = t.into, apush = function(arr, x) { arr.push(x); return var inc = function(n) { return n + 1; }; var isEven = function(n) { return n % 2 == 0; }; var xf = comp(map(inc), filter(isEven)); into([], xf, [0,1,2,3,4]); // [2,4] transduce(xf, apush, 0, [0,1,2,3,4]); // [2,4] CollReduce transducer initial value coll
  35. 108.

    [] Init: return initial value [result] Completion: some reduce operation

    require this step [result, input] Step: reducing function to build collection
  36. 111.

    Q?