othree
October 03, 2015
2.7k

# Transducer

Explain transducer in JS code snippets

October 03, 2015

## Transcript

6. ### function incOne(input) { return input + 1; } function multTwo(input)

{ return input * 2; }

9. ### z = f(y) y = g ( x ) z

= f ( g ( x ))

}; }

[2,3,4,5]

10

24

3, 4]

33. ### [1, 2, 3, 4].reduce(function(result, value) { return result + value;

}, 0); // 10 whatever whatever input

// [2, 4]
39. ### function even (input) { return (input % 2 == 0);

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

[2,3,4,5]

// [2,3,4,5]
44. ### [1,2,3,4].filter(function (input) { return (input % 2 == 0); },

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

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

Rmap,filter
54. ### map reducing function map transducer ﬁlter reducing function ﬁlter transducer

take reducing function take transducer replace reducing function replace transducer
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]; }) ) );
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]; }) ) );
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]; }) ) );
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]; }) ) );
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
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]; }) ) ); ﬁlter transducer constructor map transducer constructor
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]; }) ) ); ﬁlter predicate map transform

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

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

69. ### x g ! y f ! z coll 0 =

reduce ( z, coll )

y f ! z

x
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]
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 )
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

86. ### 0DSE\5HGXFH coll.reduce(function (result, input) { return concat(result, input + 1);

}, []) CollReduce map transform

90. ### ,QF2QH7UDQVGXFHU function (reducing) { return function (result, input) { return

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

92. ### 0DS7UDQVGXFHU&RQVWUXFWRU function mapping(transform) { return function (reducing) { return function

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

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

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

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

input) { return reducing(result, transform(input)); }; }; } whatever
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
98. ### )LOWHU7UDQVGXFHU&RQVWUXFWRU function filtering(predicate) { return function (reducing) { return function

(result, input) { return ( predicate(input) ? reducing(result, input) : result ); }; }; }
99. ### 7DNH7UDQVGXFHU&RQVWUXFWRU function taking(n) { return function (reducing) { return function

(result, input) { if (n > 0) { n--; return reducing(result, input); } else { return result; } }; }; }
100. ### 'URS7UDQVGXFHU&RQVWUXFWRU function dropping(n) { return function (reducing) { return function

(result, input) { if (n > 0) { n--; return result; } else { return reducing(result, input); } }; }; }
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]; }) ) );

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

...)))
108. ### [] Init: return initial value [result] Completion: some reduce operation

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