Slide 1

Slide 1 text

TRANSDUCERS @amitayh

Slide 2

Slide 2 text

COLLECTION TRANSFORMATIONS We use them all the time…

Slide 3

Slide 3 text

const double = x => x * 2; const isEven = x => x % 2 === 0; const coll = [1, 2, 3, 4, 5, 6];

Slide 4

Slide 4 text

const double = x => x * 2; const isEven = x => x % 2 === 0; const coll = [1, 2, 3, 4, 5, 6]; const doubled = coll.map(double); // [2, 4, 6, 8, 10, 12]

Slide 5

Slide 5 text

const double = x => x * 2; const isEven = x => x % 2 === 0; const coll = [1, 2, 3, 4, 5, 6]; const doubled = coll.map(double); const even = coll.filter(isEven); // [2, 4, 6]

Slide 6

Slide 6 text

const double = x => x * 2; const isEven = x => x % 2 === 0; const coll = [1, 2, 3, 4, 5, 6]; const doubled = coll.map(double); const even = coll.filter(isEven); const sum = coll.reduce( (acc, item) => acc + item, 0 ); // 21

Slide 7

Slide 7 text

Q: What all these have in common? A: We can de fi ne all of them in terms of reduce ⁉

Slide 8

Slide 8 text

const map = (coll, f) => { return coll.reduce( (acc, item) => [...acc, f(item)], [] ); }; const filter = (coll, pred) => { return coll.reduce( (acc, item) => pred(item) ? [...acc, item] : acc, [] ); };

Slide 9

Slide 9 text

const map = (coll, f) => { return coll.reduce( (acc, item) => [...acc, f(item)], [] ); }; const filter = (coll, pred) => { return coll.reduce( (acc, item) => pred(item) ? [...acc, item] : acc, [] ); };

Slide 10

Slide 10 text

const map = (coll, f) => { return coll.reduce( (acc, item) => [...acc, f(item)], [] ); }; const filter = (coll, pred) => { return coll.reduce( (acc, item) => pred(item) ? [...acc, item] : acc, [] ); };

Slide 11

Slide 11 text

const map = (coll, f) => { return coll.reduce( (acc, item) => [...acc, f(item)], [] ); }; const filter = (coll, pred) => { return coll.reduce( (acc, item) => pred(item) ? [...acc, item] : acc, [] ); };

Slide 12

Slide 12 text

const map = (coll, f) => { return coll.reduce( (acc, item) => [...acc, f(item)], [] ); }; const filter = (coll, pred) => { return coll.reduce( (acc, item) => pred(item) ? [...acc, item] : acc, [] ); };

Slide 13

Slide 13 text

const map = (coll, f) => { return coll.reduce( (acc, item) => [...acc, f(item)], [] ); }; const filter = (coll, pred) => { return coll.reduce( (acc, item) => pred(item) ? [...acc, item] : acc, [] ); };

Slide 14

Slide 14 text

OBSERVATIONS 👀 1. The actual logic is in the reducing function - the rest is boilerplate 2. We are coupled to our input and output types 3. Chaining several operations will introduce intermediate results - wasteful: coll.map(double).filter(isEven)

Slide 15

Slide 15 text

1⃣

Slide 16

Slide 16 text

• Problem: logic in the reducing function • Solution: extract it const map = f => (acc, item) => [...acc, f(item)]; const filter = pred => (acc, item) => pred(item) ? [...acc, item] : acc; const run = (coll, reducer) => coll.reduce(reducer, []);

Slide 17

Slide 17 text

const coll = [1, 2, 3, 4, 5, 6]; run(coll, map(double)); // [2, 4, 6, 8, 10, 12] run(coll, filter(isEven)); // [2, 4, 6]

Slide 18

Slide 18 text

2⃣

Slide 19

Slide 19 text

• Problem: coupling to input and output • Solution: inject the “step” function const map = f => (acc, item) => [...acc, f(item)]; const filter = pred => (acc, item) => pred(item) ? [...acc, item] : acc;

Slide 20

Slide 20 text

• Problem: coupling to input and output • Solution: inject the “step” function const map = f => (acc, item) => [...acc, f(item)]; const filter = pred => (acc, item) => pred(item) ? [...acc, item] : acc;

Slide 21

Slide 21 text

• Problem: coupling to input and output • Solution: inject the “step” function const map = f => step => (acc, item) => [...acc, f(item)]; const filter = pred => step => (acc, item) => pred(item) ? [...acc, item] : acc;

Slide 22

Slide 22 text

• Problem: coupling to input and output • Solution: inject the “step” function const map = f => step => (acc, item) => step(acc, f(item)); const filter = pred => step => (acc, item) => pred(item) ? step(acc, item) : acc;

Slide 23

Slide 23 text

• Problem: coupling to input and output • Solution: inject the “step” function const map = f => step => (acc, item) => step(acc, f(item)); const filter = pred => step => (acc, item) => pred(item) ? step(acc, item) : acc;

Slide 24

Slide 24 text

ENTER: TRANSDUCERS // reducer signature: (whatever, input) => whatever // transducer signature: reducer => reducer

Slide 25

Slide 25 text

const transduce = (xf, step, acc, input) => { const reducer = xf(step); for (let item of input) { acc = reducer(acc, item); } return acc; };

Slide 26

Slide 26 text

const transduce = (xf, step, acc, input) => { const reducer = xf(step); for (let item of input) { acc = reducer(acc, item); } return acc; }; // Step functions const into = (acc, item) => [...acc, item]; const sum = (acc, item) => acc + item;

Slide 27

Slide 27 text

// Some transducer const xf = filter(isEven); const coll = [1, 2, 3, 4, 5, 6]; transduce(xf, into, [], coll); // [2, 4, 6] transduce(xf, sum, 0, coll); // 12

Slide 28

Slide 28 text

FULL DECOUPLING 😎 • The process is separate from the input / output sources • Reuse transformation logic • Built in collections (arrays, objects) • Custom collections (Immutable.js) • WebSockets / In fi nite streams

Slide 29

Slide 29 text

3⃣

Slide 30

Slide 30 text

• Problem: intermediate results • Solution: function composition! const identity = x => x; const compose = (...fns) => fns.reduce( (acc, fn) => x => acc(fn(x)), identity );

Slide 31

Slide 31 text

// Composing transducers const xf = compose( filter(isEven), map(double) ); const coll = [1, 2, 3, 4, 5, 6]; // No intermediate results! transduce(xf, into, [], coll); // [4, 8, 12] transduce(xf, sum, 0, coll); // 24

Slide 32

Slide 32 text

STATEFUL TRANSDUCERS • Example: drop - remove fi rst n elements const drop = n => step => { let remaining = n; return (acc, item) => (remaining-- > 0) ? acc : step(acc, item); }; coll = [1, 2, 3, 4, 5, 6]; transduce(drop(4), into, [], coll); // [5, 6]

Slide 33

Slide 33 text

OTHER COOL TRANSDUCERS • dropWhile(pred) • partition(pred) • dedupe

Slide 34

Slide 34 text

EARLY TERMINATION const done = x => ({value: x, __done__: true});

Slide 35

Slide 35 text

EARLY TERMINATION const done = x => ({value: x, __done__: true}); const transduce = (xf, step, acc, input) => { const reducer = xf(step); for (let item of input) { acc = reducer(acc, item); } return acc; };

Slide 36

Slide 36 text

EARLY TERMINATION const done = x => ({value: x, __done__: true}); const transduce = (xf, step, acc, input) => { const reducer = xf(step); for (let item of input) { acc = reducer(acc, item); if (acc.__done__) { acc = acc.value; break; } } return acc; };

Slide 37

Slide 37 text

EARLY TERMINATION const done = x => ({value: x, __done__: true}); const transduce = (xf, step, acc, input) => { const reducer = xf(step); for (let item of input) { acc = reducer(acc, item); if (acc.__done__) { acc = acc.value; break; } } return acc; };

Slide 38

Slide 38 text

EARLY TERMINATION • Example: take - keep fi rst n elements const take = n => step => { let remaining = n; return (acc, item) => (remaining-- > 0) ? step(acc, item) : done(acc); }; transduce(take(2), into, [], coll); // [1, 2]

Slide 39

Slide 39 text

EARLY TERMINATION • Bonus! we can now use in fi nite collections function* numbers() { let index = 0; while (true) { yield index++; } }

Slide 40

Slide 40 text

EARLY TERMINATION • Bonus! we can now use in fi nite collections const xf = compose( filter(isEven), map(double), take(5) ); transduce(xf, into, [], numbers()); // [0, 4, 8, 12, 16]

Slide 41

Slide 41 text

Q&A 🤓

Slide 42

Slide 42 text

RESOURCES 📚 • Blog post: http://wix.to/G8DRABw • Talk by Rich Hickey: http://wix.to/XMDRABw • Transducers in Scala: http://wix.to/XsDRABw • …And in JavaScript: מםכאמם