Slide 1

Slide 1 text

FUNCTIONAL PROGRAMMING Jeremy Fairbank blog.jeremyfairbank.com @elpapapollo / jfairbank BASICS IN ES6

Slide 2

Slide 2 text

sigient.com Your website, SimplyBuilt. simplybuilt.com

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

¯\_(ϑ)_/¯ WHAT IS FUNCTIONAL PROGRAMMING?

Slide 5

Slide 5 text

¯\_(ϑ)_/¯ WHY FUNCTIONAL PROGRAMMING?

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Domain to Range

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Domain Range

Slide 11

Slide 11 text

CALCULUS

Slide 12

Slide 12 text

PRINCIPLES

Slide 13

Slide 13 text

PURE & DECLARATIVE PREDICTABLE

Slide 14

Slide 14 text

IMMUTABLE STATE SAFE

Slide 15

Slide 15 text

FIRST CLASS STATE TRANSPARENT

Slide 16

Slide 16 text

COMPOSABLE FIRST CLASS CLOSURES MODULAR

Slide 17

Slide 17 text

ES2015 (ES6)

Slide 18

Slide 18 text

All good let age = 28; age = 29; const name = 'Jeremy'; name = 'Jet'; Syntax error

Slide 19

Slide 19 text

const add = (x, y) => { return x + y; }; const identity = x => x;

Slide 20

Slide 20 text

const add = (x, y) => { return x + y; }; const identity = x => x;

Slide 21

Slide 21 text

const array = (...elements) => { return elements; }; array(1, 2, 3); // [1, 2, 3] const log = (...args) => { console.log(...args); }; log('hello', 'scenic city summit'); // hello scenic city summit

Slide 22

Slide 22 text

const array = (...elements) => { return elements; }; array(1, 2, 3); // [1, 2, 3] const log = (...args) => { console.log(...args); }; log('hello', 'scenic city summit'); // hello scenic city summit

Slide 23

Slide 23 text

const langs = ['JavaScript', 'Ruby', 'Haskell']; const [js, ...rest] = langs; js === 'JavaScript'; rest[0] === 'Ruby'; rest[1] === 'Haskell'; const head = ([x]) => x; head([1, 2, 3]) === 1;

Slide 24

Slide 24 text

const langs = ['JavaScript', 'Ruby', 'Haskell']; const [js, ...rest] = langs; js === 'JavaScript'; rest[0] === 'Ruby'; rest[1] === 'Haskell'; const head = ([x]) => x; head([1, 2, 3]) === 1;

Slide 25

Slide 25 text

const greet = (name, greeting = 'Hi') => { console.log(greeting, name); }; greet('Scenic City Summit', 'Hello'); // Hello Scenic City Summit greet('Chattanooga'); // Hi Chattanooga

Slide 26

Slide 26 text

Object.assign( {}, { hello: 'Chattanooga' }, { hi: 'Scenic City Summit' } ); // { // hello: 'Chattanooga', // hi: 'Scenic City Summit' // }

Slide 27

Slide 27 text

class Point { constructor(x, y) { this.x = x; this.y = y; } moveBy(dx, dy) { this.x += dx; this.y += dy; } } function Point(x, y) { this.x = x; this.y = y; } Point.prototype.moveBy = function(dx, dy) { this.x += dx; this.y += dy; };

Slide 28

Slide 28 text

PURE

Slide 29

Slide 29 text

const add = (x, y) => x + y; add(2, 3) === 5; add(2, 3) === 5; add(2, 3) === 5;

Slide 30

Slide 30 text

const add = (x, y) => x + y; add(2, 3) === 5; add(2, 3) === 5; add(2, 3) === 5; Referentially transparent

Slide 31

Slide 31 text

× let name = 'Jeremy'; const getName = () => name; const setName = (newName) => { name = newName; }; const printUpperName = () => { console.log(name.toUpperCase()); };

Slide 32

Slide 32 text

describe('api', () => { beforeEach(() => mockConsoleLog()); afterEach(() => restoreConsoleLog()); it('sets and prints the name', () => { printUpperName(); expect(console.log).calledWith('JEREMY'); setName('Jet'); printUpperName(); expect(console.log).calledWith('JET'); }); }); ×

Slide 33

Slide 33 text

HIDDEN STATE IS UNCERTAIN STATE

Slide 34

Slide 34 text

const upperName = (name) => name.toUpperCase(); describe('api', () => { it('returns an uppercase name', () => { expect(upperName('Jeremy')).to.equal('JEREMY'); expect(upperName('Jet')).to.equal('JET'); }); }); ✓

Slide 35

Slide 35 text

HOW TO ACHIEVE THE RESULT IMPERATIVE

Slide 36

Slide 36 text

function doubleNumbers(numbers) { const doubled = []; const l = numbers.length; for (let i = 0; i < l; i++) { doubled.push(numbers[i] * 2); } return doubled; } doubleNumbers([1, 2, 3]); // [2, 4, 6]

Slide 37

Slide 37 text

DECLARE WHAT THE DESIRED RESULT IS DECLARATIVE

Slide 38

Slide 38 text

function doubleNumbers(numbers) { return numbers.map(n => n * 2); } doubleNumbers([1, 2, 3]); // [2, 4, 6]

Slide 39

Slide 39 text

1 2 3 numbers.map(n => n * 2) 2 4 6 Domain Range

Slide 40

Slide 40 text

CREATE STATE, DON’T MUTATE IT IMMUTABLE

Slide 41

Slide 41 text

const hobbies = [ 'programming', 'reading', 'music' ]; const firstTwo = hobbies.splice(0, 2); console.log(firstTwo); // ['programming', 'reading'] console.log(hobbies); // ['music']

Slide 42

Slide 42 text

const hobbies = [ 'programming', 'reading', 'music' ]; const firstTwo = hobbies.splice(0, 2); console.log(firstTwo); // ['programming', 'reading'] console.log(hobbies); // ['music'] Slight typo + mutability

Slide 43

Slide 43 text

Object.freeze Immutable.js

Slide 44

Slide 44 text

const hobbies = Object.freeze([ 'programming', 'reading', 'music' ]); const firstTwo = hobbies.splice(0, 2); // TypeError

Slide 45

Slide 45 text

FREE YOUR STATE

Slide 46

Slide 46 text

class Point { constructor(x, y) { this.x = x; this.y = y; } moveBy(dx, dy) { this.x += dx; this.y += dy; } } const point = new Point(0, 0); point.moveBy(5, 5); point.moveBy(-2, 2); console.log([point.x, point.y]); // [3, 7] ×

Slide 47

Slide 47 text

const createPoint = (x, y) => Object.freeze([x, y]); const movePointBy = ([x, y], dx, dy) => { return Object.freeze([x + dx, y + dy]); }; let point = createPoint(0, 0); point = movePointBy(point, 5, 5); point = movePointBy(point, -2, 2); console.log(point); // [3, 7] ✓ (*or object-oriented without mutation)

Slide 48

Slide 48 text

PROS SAFETY FREE UNDO/REDO LOGS — REDUX EXPLICIT FLOW OF DATA LESS MEMORY USAGE* CONCURRENCY SAFETY* *In certain cases

Slide 49

Slide 49 text

CONS VERBOSE MORE OBJECT CREATION* MORE GARBAGE COLLECTION* MORE MEMORY USAGE* *Alleviated with libs like Immutable.js

Slide 50

Slide 50 text

FIRST CLASS FUNCTIONS

Slide 51

Slide 51 text

const multiply = (x, y) => x * y; function add(x, y) { return x + y; } const addAlias = add; const evens = [1, 2, 3].map(n => n * 2);

Slide 52

Slide 52 text

const multiply = (x, y) => x * y; function add(x, y) { return x + y; } const addAlias = add; const evens = [1, 2, 3].map(n => n * 2);

Slide 53

Slide 53 text

const multiply = (x, y) => x * y; function add(x, y) { return x + y; } const addAlias = add; const evens = [1, 2, 3].map(n => n * 2);

Slide 54

Slide 54 text

const multiply = (x, y) => x * y; function add(x, y) { return x + y; } const addAlias = add; const evens = [1, 2, 3].map(n => n * 2);

Slide 55

Slide 55 text

ENCAPSULATION CLOSURES

Slide 56

Slide 56 text

const createAdder = (x) => { return (y) => x + y; }; const add3 = createAdder(3); add3(2) === 5; add3(3) === 6;

Slide 57

Slide 57 text

const createAdder = (x) => { return (y) => x + y; }; const add3 = createAdder(3); add3(2) === 5; add3(3) === 6;

Slide 58

Slide 58 text

const request = (options) => { return fetch(options.url, options) .then(resp => resp.json()); }; const usersPromise = request({ url: '/users', headers: { 'X-Custom': 'mykey' } }); const tasksPromise = request({ url: '/tasks', headers: { 'X-Custom': 'mykey' } });

Slide 59

Slide 59 text

const request = (options) => { return fetch(options.url, options) .then(resp => resp.json()); }; const usersPromise = request({ url: '/users', headers: { 'X-Custom': 'mykey' } }); const tasksPromise = request({ url: '/tasks', headers: { 'X-Custom': 'mykey' } }); Repetitive

Slide 60

Slide 60 text

const createRequester = (options) => { return (otherOptions) => { return request(Object.assign( {}, options, otherOptions )); }; }; const customRequest = createRequester({ headers: { 'X-Custom': 'mykey' } }); const usersPromise = customRequest({ url: '/users' }); const tasksPromise = customRequest({ url: '/tasks' });

Slide 61

Slide 61 text

const createRequester = (options) => { return (otherOptions) => { return request(Object.assign( {}, options, otherOptions )); }; }; const customRequest = createRequester({ headers: { 'X-Custom': 'mykey' } }); const usersPromise = customRequest({ url: '/users' }); const tasksPromise = customRequest({ url: '/tasks' });

Slide 62

Slide 62 text

FOUNDATION FOR HIGHER ORDER PATTERNS FIRST CLASS CLOSURES

Slide 63

Slide 63 text

PARTIAL APPLICATION

Slide 64

Slide 64 text

const createAdder = (x) => { return (y) => x + y; }; const createRequester = (options) => { return (otherOptions) => { return request(Object.assign( {}, options, otherOptions )); }; }; RECALL

Slide 65

Slide 65 text

const add = (x, y) => x + y; const add3 = partial(add, 3); add3(2) === 5;

Slide 66

Slide 66 text

const request = (defaults, options) => { options = Object.assign({}, defaults, options); return fetch(options.url, options) .then(resp => resp.json()); }; const customRequest = partial(request, { headers: { 'X-Custom': 'mykey' } }); const usersPromise = customRequest({ url: '/users' }); const tasksPromise = customRequest({ url: '/tasks' });

Slide 67

Slide 67 text

const partialFromBind = (fn, ...args) => { return fn.bind(null, ...args); }; const partial = (fn, ...args) => { return (...otherArgs) => { return fn(...args, ...otherArgs) }; };

Slide 68

Slide 68 text

const partialFromBind = (fn, ...args) => { return fn.bind(null, ...args); }; const partial = (fn, ...args) => { return (...otherArgs) => { return fn(...args, ...otherArgs) }; };

Slide 69

Slide 69 text

const partialFromBind = (fn, ...args) => { return fn.bind(null, ...args); }; const partial = (fn, ...args) => { return (...otherArgs) => { return fn(...args, ...otherArgs) }; };

Slide 70

Slide 70 text

const partialFromBind = (fn, ...args) => { return fn.bind(null, ...args); }; const partial = (fn, ...args) => { return (...otherArgs) => { return fn(...args, ...otherArgs) }; };

Slide 71

Slide 71 text

const partialFromBind = (fn, ...args) => { return fn.bind(null, ...args); }; const partial = (fn, ...args) => { return (...otherArgs) => { return fn(...args, ...otherArgs) }; };

Slide 72

Slide 72 text

const partialFromBind = (fn, ...args) => { return fn.bind(null, ...args); }; const partial = (fn, ...args) => { return (...otherArgs) => { return fn(...args, ...otherArgs) }; };

Slide 73

Slide 73 text

CURRYING

Slide 74

Slide 74 text

const add3 = add(3); add3(2) === 5; const customRequest = request({ headers: { 'X-Custom': 'mykey' } }); const usersPromise = customRequest({ url: '/users' }); const tasksPromise = customRequest({ url: '/tasks' });

Slide 75

Slide 75 text

const add3 = add(3); add3(2) === 5; const customRequest = request({ headers: { 'X-Custom': 'mykey' } }); const usersPromise = customRequest({ url: '/users' }); const tasksPromise = customRequest({ url: '/tasks' });

Slide 76

Slide 76 text

const add = x => y => x + y; function add(x) { return function(y) { return x + y; }; }

Slide 77

Slide 77 text

const add = x => y => x + y; function add(x) { return function(y) { return x + y; }; }

Slide 78

Slide 78 text

const add = x => y => x + y; function add(x) { return function(y) { return x + y; }; }

Slide 79

Slide 79 text

const request = defaults => options => { options = Object.assign( {}, defaults, options ); return fetch(options.url, options) .then(resp => resp.json()); };

Slide 80

Slide 80 text

const request = defaults => options => { options = Object.assign( {}, defaults, options ); return fetch(options.url, options) .then(resp => resp.json()); };

Slide 81

Slide 81 text

PIECING IT TOGETHER

Slide 82

Slide 82 text

const map = fn => array => array.map(fn); const multiply = x => y => x * y; const pluck = key => object => object[key]; const discount = multiply(0.98); const tax = multiply(1.0925); const customRequest = request({ headers: { 'X-Custom': 'mykey' } }); customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax));

Slide 83

Slide 83 text

const map = fn => array => array.map(fn); const multiply = x => y => x * y; const pluck = key => object => object[key]; const discount = multiply(0.98); const tax = multiply(1.0925); const customRequest = request({ headers: { 'X-Custom': 'mykey' } }); customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax));

Slide 84

Slide 84 text

const map = fn => array => array.map(fn); const multiply = x => y => x * y; const pluck = key => object => object[key]; const discount = multiply(0.98); const tax = multiply(1.0925); const customRequest = request({ headers: { 'X-Custom': 'mykey' } }); customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax));

Slide 85

Slide 85 text

const map = fn => array => array.map(fn); const multiply = x => y => x * y; const pluck = key => object => object[key]; const discount = multiply(0.98); const tax = multiply(1.0925); const customRequest = request({ headers: { 'X-Custom': 'mykey' } }); customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax));

Slide 86

Slide 86 text

const map = fn => array => array.map(fn); const multiply = x => y => x * y; const pluck = key => object => object[key]; const discount = multiply(0.98); const tax = multiply(1.0925); const customRequest = request({ headers: { 'X-Custom': 'mykey' } }); customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax));

Slide 87

Slide 87 text

const map = fn => array => array.map(fn); const multiply = x => y => x * y; const pluck = key => object => object[key]; const discount = multiply(0.98); const tax = multiply(1.0925); const customRequest = request({ headers: { 'X-Custom': 'mykey' } }); customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax));

Slide 88

Slide 88 text

const map = fn => array => array.map(fn); const multiply = x => y => x * y; const pluck = key => object => object[key]; const discount = multiply(0.98); const tax = multiply(1.0925); const customRequest = request({ headers: { 'X-Custom': 'mykey' } }); customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax));

Slide 89

Slide 89 text

customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax)); [ { price: 5 }, { price: 10 }, { price: 3 } ]

Slide 90

Slide 90 text

[ { price: 5 }, { price: 10 }, { price: 3 } ] map(pluck('price')) [ 5, 10, 3 ] item.price

Slide 91

Slide 91 text

[ 5, 10, 3 ] map(discount) [ 4.9, 9.8, 2.94 ] price * 0.98

Slide 92

Slide 92 text

[ 5.35, 10.71, 3.21 ] map(tax) [ 4.9, 9.8, 2.94 ] price * 1.0925

Slide 93

Slide 93 text

COMPOSING CLOSURES

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

const processWord = compose(hyphenate, reverse, toUpperCase); const words = [ 'hello', 'functional', 'programming' ]; const newWords = words.map(processWord); console.log(newWords); // ['OL-LEH, 'LANOI-TCNUF', 'GNIMM-ARGORP']

Slide 96

Slide 96 text

const processWord = compose(hyphenate, reverse, toUpperCase); const words = [ 'hello', 'functional', 'programming' ]; const newWords = words.map(processWord); console.log(newWords); // ['OL-LEH, 'LANOI-TCNUF', 'GNIMM-ARGORP']

Slide 97

Slide 97 text

const processWord = compose(hyphenate, reverse, toUpperCase); const processWordExplicit = (word) => { return hyphenate(reverse(toUpperCase(word))); };

Slide 98

Slide 98 text

customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax)); RETURNING TO PRICES EXAMPLE

Slide 99

Slide 99 text

customRequest({ url: '/cart/items' }) .then(map(pluck('price'))) .then(map(discount)) .then(map(tax)); Triple iteration RETURNING TO PRICES EXAMPLE

Slide 100

Slide 100 text

customRequest({ url: '/cart/items' }) .then(map( compose( tax, discount, pluck('price') ) )); Single iteration

Slide 101

Slide 101 text

const compose = (...fns) => (...args) => { if (fns.length === 0) { return args[0]; } const last = fns[fns.length - 1]; const rest = fns.slice(0, -1); return rest.reduceRight((memo, fn) => { return fn(memo); }, last(...args)); };

Slide 102

Slide 102 text

const compose = (...fns) => (...args) => { if (fns.length === 0) { return args[0]; } const last = fns[fns.length - 1]; const rest = fns.slice(0, -1); return rest.reduceRight((memo, fn) => { return fn(memo); }, last(...args)); };

Slide 103

Slide 103 text

const compose = (...fns) => (...args) => { if (fns.length === 0) { return args[0]; } const last = fns[fns.length - 1]; const rest = fns.slice(0, -1); return rest.reduceRight((memo, fn) => { return fn(memo); }, last(...args)); };

Slide 104

Slide 104 text

const compose = (...fns) => (...args) => { if (fns.length === 0) { return args[0]; } const last = fns[fns.length - 1]; const rest = fns.slice(0, -1); return rest.reduceRight((memo, fn) => { return fn(memo); }, last(...args)); };

Slide 105

Slide 105 text

RECURSION SOLVE A PROBLEM IN TERMS OF ITSELF

Slide 106

Slide 106 text

FACTORIAL

Slide 107

Slide 107 text

const factorial = (n) => { let result = 1; while (n > 1) { result *= n; n--; } return result; };

Slide 108

Slide 108 text

const factorial = (n) => { if (n < 2) { return 1; } return n * factorial(n - 1); };

Slide 109

Slide 109 text

const factorial = (n) => { if (n < 2) { return 1; } return n * factorial(n - 1); }; Recursive call

Slide 110

Slide 110 text

const factorial = (n) => { if (n < 2) { return 1; } return n * factorial(n - 1); }; Base case

Slide 111

Slide 111 text

factorial(4);

Slide 112

Slide 112 text

factorial(4); 4 * factorial(3);

Slide 113

Slide 113 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2);

Slide 114

Slide 114 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1);

Slide 115

Slide 115 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1;

Slide 116

Slide 116 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2;

Slide 117

Slide 117 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6;

Slide 118

Slide 118 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24;

Slide 119

Slide 119 text

STEPS Find the recurrence
 (n × n-1 × n-2 × … 1)
 Find the base case
 (n < 2)

Slide 120

Slide 120 text

PERFORMANCE RECURSION

Slide 121

Slide 121 text

const value = factorial(100000); console.log(value); // ??? WHAT IS THE RESULT?

Slide 122

Slide 122 text

const value = factorial(100000); console.log(value); // ??? WHAT IS THE RESULT? RangeError: Maximum call stack size exceeded

Slide 123

Slide 123 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24; Call Stack

Slide 124

Slide 124 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24; factorial(4) Call Stack

Slide 125

Slide 125 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24; factorial(4) factorial(3) Call Stack

Slide 126

Slide 126 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24; factorial(4) factorial(3) factorial(2) Call Stack

Slide 127

Slide 127 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24; factorial(4) factorial(3) factorial(2) factorial(1) Call Stack

Slide 128

Slide 128 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24; factorial(4) factorial(3) factorial(2) Call Stack

Slide 129

Slide 129 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24; factorial(4) factorial(3) Call Stack

Slide 130

Slide 130 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24; factorial(4) Call Stack

Slide 131

Slide 131 text

factorial(4); 4 * factorial(3); 4 * 3 * factorial(2); 4 * 3 * 2 * factorial(1); 4 * 3 * 2 * 1; 4 * 3 * 2; 4 * 6; 24; Call Stack

Slide 132

Slide 132 text

const value = factorial(100000); console.log(value); // ??? 100,000 calls = 100,000 stack frames 1 stack frame ≈ 48B Max stack usage ≈ 1MB 100,000 x 48 / 1024 / 1024 = 4.58MB > 1MB

Slide 133

Slide 133 text

IN ES2015! TAIL CALL OPTIMIZATION

Slide 134

Slide 134 text

const factorial = (n) => { if (n < 2) { return 1; } return n * factorial(n - 1); }; UNOPTIMIZABLE

Slide 135

Slide 135 text

const factorial = (n) => { if (n < 2) { return 1; } return n * factorial(n - 1); }; 1 UNOPTIMIZABLE

Slide 136

Slide 136 text

const factorial = (n) => { if (n < 2) { return 1; } return n * factorial(n - 1); }; 1 UNOPTIMIZABLE 2

Slide 137

Slide 137 text

const factorial = (n) => { if (n < 2) { return 1; } return n * factorial(n - 1); }; 1 UNOPTIMIZABLE 2 3

Slide 138

Slide 138 text

const factorial = (n, accum = 1) => { if (n < 2) { return accum; } return factorial(n - 1, n * accum); }; OPTIMIZABLE

Slide 139

Slide 139 text

const factorial = (n, accum = 1) => { if (n < 2) { return accum; } return factorial(n - 1, n * accum); }; OPTIMIZABLE

Slide 140

Slide 140 text

const factorial = (n, accum = 1) => { if (n < 2) { return accum; } return factorial(n - 1, n * accum); }; OPTIMIZABLE

Slide 141

Slide 141 text

const factorial = (n, accum = 1) => { if (n < 2) { return accum; } return factorial(n - 1, n * accum); }; OPTIMIZABLE

Slide 142

Slide 142 text

const factorial = (n, accum = 1) => { if (n < 2) { return accum; } return factorial(n - 1, n * accum); }; 1 OPTIMIZABLE

Slide 143

Slide 143 text

const factorial = (n, accum = 1) => { if (n < 2) { return accum; } return factorial(n - 1, n * accum); }; 1 2 OPTIMIZABLE

Slide 144

Slide 144 text

const factorial = (n, accum = 1) => { if (n < 2) { return accum; } return factorial(n - 1, n * accum); }; 1 2 3 OPTIMIZABLE

Slide 145

Slide 145 text

const value = factorial(100000); console.log(value); // Infinity

Slide 146

Slide 146 text

factorial(4 /*, 1 */); factorial(3, 4); factorial(2, 12); factorial(1, 24); 24; Call Stack

Slide 147

Slide 147 text

factorial(4 /*, 1 */); factorial(3, 4); factorial(2, 12); factorial(1, 24); 24; factorial(4, 1) Call Stack

Slide 148

Slide 148 text

factorial(4 /*, 1 */); factorial(3, 4); factorial(2, 12); factorial(1, 24); 24; factorial(3, 4) Call Stack

Slide 149

Slide 149 text

factorial(4 /*, 1 */); factorial(3, 4); factorial(2, 12); factorial(1, 24); 24; factorial(2, 12) Call Stack

Slide 150

Slide 150 text

factorial(4 /*, 1 */); factorial(3, 4); factorial(2, 12); factorial(1, 24); 24; factorial(1, 24) Call Stack

Slide 151

Slide 151 text

factorial(4 /*, 1 */); factorial(3, 4); factorial(2, 12); factorial(1, 24); 24; Call Stack

Slide 152

Slide 152 text

RECAP PREDICTABLE SAFE TRANSPARENT MODULAR

Slide 153

Slide 153 text

RESOURCES

Slide 154

Slide 154 text

drboolean.gitbooks.io/mostly-adequate-guide Brian Lonsdorf

Slide 155

Slide 155 text

ES6/7/LATER babeljs.io

Slide 156

Slide 156 text

LANGUAGES Elm (elm-lang.org) Clojurescript (github.com/clojure/clojurescript) Purescript (purescript.org)

Slide 157

Slide 157 text

LIBRARIES Lodash (lodash.com) Ramda (ramdajs.com) Rx (reactivex.io) Bacon.js (baconjs.github.io) Immutable.js (facebook.github.io/immutable-js)

Slide 158

Slide 158 text

“MV*” React (facebook.github.io/react) Redux (redux.js.org)

Slide 159

Slide 159 text

THANKS! Jeremy Fairbank blog.jeremyfairbank.com @elpapapollo / jfairbank Code: github.com/jfairbank/fp-basics-in-es6