Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

A Unified Theory of JavaScript Style, Part I JavaScript Combinators

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

we'll talk about Combinators and decorators

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

but think about Flexibility and decluttering

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

composition We compose entities to create new entities

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

interfaces Not all entities "fit together"

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Homogeneous interfaces create dense spaces

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Heterogeneous interfaces create sparse spaces

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Dense is more flexible than sparse

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Sparse can be quicker to grasp

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

enough with the math!

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

pluck: "A convenient version of what is perhaps the most common use-case for map: extracting a list of property values."

Slide 23

Slide 23 text

"pluckWith" is the flipped form of "pluck"

Slide 24

Slide 24 text

function pluck (mappable, key) { return mappable.map(function (obj) { return obj[key]; }); }; function pluckWith (key, mappable) { return pluck(mappable, key); }; var stooges = [ {name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}]; pluckWith('name', stooges); //=> ["moe", "larry", "curly"]

Slide 25

Slide 25 text

Let's make "pluckWith" out of combinators

Slide 26

Slide 26 text

A unary combinator function flip (fn) { return function flipped (a, b) { return fn.call(this, b, a); } } function arrow (a, b) { return "" + a + " -> " + b; } flip(arrow)("x", "y") //=> 'y -> x'

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

curry Another unary combinator

Slide 29

Slide 29 text

function curry (fn) { return function curried (a, optionalB) { if (arguments.length > 1) { return fn.call(this, a, optionalB); } else return function partiallyApplied (b) { return fn.call(this, a, b); } } }

Slide 30

Slide 30 text

Currying: var curriedArrow = curry(arrow); //=> [function] curriedArrow('finger')('moon') //=> 'finger -> moon'

Slide 31

Slide 31 text

Partial Application: var taoism = curry(arrow)('finger'); //=> [function] taoism('moon') //=> 'finger -> moon'

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

nota bene Partial application transforms binary operations into unary operations

Slide 34

Slide 34 text

function get (object, property) { return object[property]; } get({foo: 1}, 'foo') //=> 1

Slide 35

Slide 35 text

var getWith = curry(flip(get)); getWith('foo')({foo: 1}) //=> 1

Slide 36

Slide 36 text

function map (mappable, fn) { return mappable.map(fn, this); } function double (n) { return n * 2; } map([1, 2, 3], double) //=> [2, 4, 6]

Slide 37

Slide 37 text

var mapWith = curry(flip(map)); mapWith(double, [1, 2, 3]); //=> [2, 4, 6] var doubleAll = mapWith(double); doubleAll([1, 2, 3]) //=> [2, 4, 6]

Slide 38

Slide 38 text

“almost there...”

Slide 39

Slide 39 text

function pluckWith (attr) { return mapWith(getWith(attr)); }

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Compose

Slide 42

Slide 42 text

function compose (a, b) { return function composed (c) { return a(b(c)); } }

Slide 43

Slide 43 text

quod erat demonstrandum The combinator implementation of "pluckWith"

Slide 44

Slide 44 text

var pluckWith = compose(mapWith, getWith);

Slide 45

Slide 45 text

Let's compare both implementations of "pluckWith"

Slide 46

Slide 46 text

var pluckWith = compose(mapWith, getWith); //// versus //// function pluck (mappable, key) { return mappable.map(function (obj) { return obj[key]; }); }; function pluckWith (key, mappable) { return pluck(mappable, key); };

Slide 47

Slide 47 text

lesson Composing functions with combinators increases code flexibility...

Slide 48

Slide 48 text

lesson Composing functions with combinators demands increased mental flexibility

Slide 49

Slide 49 text

using combinators to make Decorators

Slide 50

Slide 50 text

function Cake () {} extend(Cake.prototype, { mix: function () { // mix ingredients together return this; }, rise: function (duration) { // let the ingredients rise return this; }, bake: function () { // do some baking return this; } });

Slide 51

Slide 51 text

fluent function fluent (methodBody) { return function fluentized () { methodBody.apply(this, arguments); return this; } }

Slide 52

Slide 52 text

function Cake () {} extend(Cake.prototype, { mix: fluent( function () { // mix ingredients together }), rise: fluent( function (duration) { // let the ingredients rise }), bake: fluent(function () { // do some baking }) });

Slide 53

Slide 53 text

new requirements Mix before rising or baking

Slide 54

Slide 54 text

extend(Cake.prototype, { mix: fluent( function () { // mix ingredients together }), rise: fluent( function (duration) { this.mix(); // let the ingredients rise }), bake: fluent(function () { this.mix(); // do some baking }) });

Slide 55

Slide 55 text

before a combinator that transforms decorations into decorators var before = curry( function decorate (decoration, method) { return function decoratedWithBefore () { decoration.apply(this, arguments); return method.apply(this, arguments); }; } ); var mixFirst = before(function () { this.mix() });

Slide 56

Slide 56 text

the final version extend(Cake.prototype, { // Other methods... mix: fluent( function () { // mix ingredients together }), rise: fluent( mixFirst( function (duration) { // let the ingredients rise })), bake: fluent( mixFirst( function () { // do some baking })) });

Slide 57

Slide 57 text

lesson Decorators declutter secondary concerns

Slide 58

Slide 58 text

after var after = curry( function decorate (decoration, method) { return function decoratedWithAfter () { var returnValue = method.apply(this, arguments); decoration.apply(this, arguments); return returnValue; }; } );

Slide 59

Slide 59 text

around var around = curry( function decorate (decoration, method) { return function decoratedWithAround () { var methodPrepended = [method].concat( [].slice.call(arguments, 0) ); return decoration.apply(this, methodPrepended); }; } );

Slide 60

Slide 60 text

call me maybe var maybe = around(function (fn, value) { if (value != null) { return fn.call(this, value);; } }); maybe(double)(2) //=> 4 maybe(double)(null) //=> undefined

Slide 61

Slide 61 text

generalized guards function provided (guard) { return around(function () { var fn = arguments[0], values = [].slice.call(arguments, 1); if (guard.apply(this, values)) { return fn.apply(this, values); } }); } var maybe = provided( function (value) { return value != null; });

Slide 62

Slide 62 text

inversions function not (fn) { return function notted () { return !fn.apply(this, arguments) } } var except = compose(provided, not); var maybe = except( function (value) { return value == null; });

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

lessons

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

lesson one Combinators increase code flexibility and require increased mental flexibility

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

lesson two Decorators declutter secondary concerns

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

lesson three Do not follow in the footsteps of the sages. Seek what they sought

Slide 71

Slide 71 text

Reginald Braithwaite GitHub, Inc. raganwald.com @raganwald NDC Conference, Oslo, Norway, June 5, 2014