Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Transducer

othree
October 03, 2015

 Transducer

Explain transducer in JS code snippets

othree

October 03, 2015
Tweet

More Decks by othree

Other Decks in Technology

Transcript

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

    = f ( g ( x ))
  2. function even (input) { return (input % 2 == 0);

    } [1,2,3,4].filter(even); // [2, 4]
  3. [1,2,3,4].reduce(function (result, input) { return ( input % 2 ==

    0 ? concat(result, input) : result ); }, []) // => [2, 4]
  4. map reducing function map transducer filter reducing function filter transducer

    take reducing function take transducer replace reducing function replace transducer
  5. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. x g ! y f ! z coll 0 =

    reduce ( z, coll )
  16. 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]
  17. 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 )
  18. 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
  19. +RZ

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

    reducing(result, input + 1); }; } map transform CollReduce
  21. 0DS7UDQVGXFHU&RQVWUXFWRU function mapping(transform) { return function (reducing) { return function

    (result, input) { return reducing(result, transform(input)); }; }; }
  22. function mapping(transform) { return function (reducing) { return function (result,

    input) { return reducing(result, transform(input)); }; }; } transducer constructor
  23. function mapping(transform) { return function (reducing) { return function (result,

    input) { return reducing(result, transform(input)); }; }; } transducer x g ! y f ! z g
  24. function mapping(transform) { return function (reducing) { return function (result,

    input) { return reducing(result, transform(input)); }; }; } reducing function
  25. function mapping(transform) { return function (reducing) { return function (result,

    input) { return reducing(result, transform(input)); }; }; } whatever
  26. 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
  27. )LOWHU7UDQVGXFHU&RQVWUXFWRU function filtering(predicate) { return function (reducing) { return function

    (result, input) { return ( predicate(input) ? reducing(result, input) : result ); }; }; }
  28. 7DNH7UDQVGXFHU&RQVWUXFWRU function taking(n) { return function (reducing) { return function

    (result, input) { if (n > 0) { n--; return reducing(result, input); } else { return result; } }; }; }
  29. 'URS7UDQVGXFHU&RQVWUXFWRU function dropping(n) { return function (reducing) { return function

    (result, input) { if (n > 0) { n--; return result; } else { return reducing(result, input); } }; }; }
  30. 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]; }) ) );
  31. 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]
  32. 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
  33. [] Init: return initial value [result] Completion: some reduce operation

    require this step [result, input] Step: reducing function to build collection
  34. Q?