Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CodeStock: Functional Programming Basics in JavaScript

CodeStock: Functional Programming Basics in JavaScript

Jeremy Fairbank

July 11, 2015
Tweet

More Decks by Jeremy Fairbank

Other Decks in Programming

Transcript

  1. Functional Programming Basics in JavaScript
    2:40 PM / Ballroom A

    View Slide

  2. Functional Programming Basics
    in JavaScript
    Jeremy Fairbank
    @elpapapollo

    View Slide

  3. Understand, apply, and create basic
    functional programming tenets and
    constructs in JavaScript.

    View Slide

  4. Hi, I’m Jeremy
    jfairbank
    @elpapapollo
    pushagency.io
    blog.jeremyfairbank.com
    simplybuilt.com

    View Slide

  5. What is a function?

    View Slide

  6. What is a function?

    View Slide

  7. What is a function?
    Relation that pairs each
    element in the domain with
    exactly one element in the
    range.
    Domain Range

    View Slide

  8. What is a function?

    View Slide

  9. What is a function?

    View Slide

  10. What is a function?

    View Slide

  11. What is a function?
    Domain

    View Slide

  12. What is a function?
    Domain Range

    View Slide

  13. Unique mapping from input to output!
    Domain Range

    View Slide

  14. Ugh, math?

    View Slide

  15. Alonzo Church

    View Slide

  16. Alonzo Church
    • Church-Turing Thesis
    • Lambda Calculus
    • Represent computation in
    terms of “function abstraction
    and application using variable
    binding and substitution.”

    View Slide

  17. λ-Calculus
    Represent computation in terms of “function abstraction and
    application using variable binding and substitution.”

    View Slide

  18. Represent computation in terms of “function abstraction and
    application using variable binding and substitution.”
    λ-Calculus

    View Slide

  19. Represent computation in terms of “function abstraction and
    application using variable binding and substitution.”
    λ-Calculus
    Bind “x” to function => parameters

    View Slide

  20. Represent computation in terms of “function abstraction and
    application using variable binding and substitution.”
    λ-Calculus
    Substitution => apply arguments

    View Slide

  21. λ-Calculus
    Functions are anonymous.

    View Slide

  22. Functions are anonymous.
    λ-Calculus

    View Slide

  23. Functions are anonymous.
    λ-Calculus
    X

    View Slide

  24. Functions are anonymous.
    λ-Calculus
    Functions are expressions and units of
    computation.

    View Slide

  25. λ-Calculus
    Functions are single input.

    View Slide

  26. Functions are single input.
    λ-Calculus

    View Slide

  27. Functions are single input.
    λ-Calculus

    View Slide

  28. Functions are single input.
    λ-Calculus
    Break down into smaller pieces — currying!

    View Slide

  29. That’s nice.
    What about functional
    programming?

    View Slide

  30. λ-Calculus shows the power of
    functional computation.
    Functional programming brings it to
    life in computer science!

    View Slide

  31. What is Functional
    Programming?

    View Slide

  32. – Wikipedia
    “In computer science, functional programming
    is a programming paradigm — a style of
    building the structure and elements of
    computer programs — that treats
    computation as the evaluation of
    mathematical functions and avoids changing-
    state and mutable data. It is a declarative
    programming paradigm, which means
    programming is done with expressions.”
    What is Functional Programming?

    View Slide

  33. TL;DR
    Programming without assignment statements.

    View Slide

  34. Key Concepts
    • Declarative (vs. Imperative)
    • First Class Functions
    • Referential Transparency and Purity
    • Recursion
    • Immutability
    • Partial Application and Currying
    • Composition

    View Slide

  35. Thar be ES2015 ahead!

    View Slide

  36. const greeting = 'hello';
    let n = 42;
    n = 5;
    const add = (x, y) => x + y;
    const printAndReturn = (string) => {
    console.log(string);
    return string;
    };
    function greet(g = 'Hi') {
    console.log(g);
    }
    function print(a, ...args) {
    console.log(a, ...args);
    }
    const [x, ...y] = ['foo', 'bar', 'baz'];
    console.log(x); // foo
    console.log(y); // ['bar', 'baz']

    View Slide

  37. var greeting = 'hello';
    const greeting = 'hello';
    let n = 42;
    n = 5;
    const add = (x, y) => x + y;
    const printAndReturn = (string) => {
    console.log(string);
    return string;
    };
    function greet(g = 'Hi') {
    console.log(g);
    }
    function print(a, ...args) {
    console.log(a, ...args);
    }
    const [x, ...y] = ['foo', 'bar', 'baz'];
    console.log(x); // foo
    console.log(y); // ['bar', 'baz']

    View Slide

  38. var n = 42;
    n = 5;
    const greeting = 'hello';
    let n = 42;
    n = 5;
    const add = (x, y) => x + y;
    const printAndReturn = (string) => {
    console.log(string);
    return string;
    };
    function greet(g = 'Hi') {
    console.log(g);
    }
    function print(a, ...args) {
    console.log(a, ...args);
    }
    const [x, ...y] = ['foo', 'bar', 'baz'];
    console.log(x); // foo
    console.log(y); // ['bar', 'baz']

    View Slide

  39. const greeting = 'hello';
    let n = 42;
    n = 5;
    const add = (x, y) => x + y;
    const printAndReturn = (string) => {
    console.log(string);
    return string;
    };
    function greet(g = 'Hi') {
    console.log(g);
    }
    function print(a, ...args) {
    console.log(a, ...args);
    }
    const [x, ...y] = ['foo', 'bar', 'baz'];
    console.log(x); // foo
    console.log(y); // ['bar', 'baz']
    var add = function(x, y) {
    return x + y;
    };
    var printAndReturn = function(string) {
    console.log(string);
    return string;
    };

    View Slide

  40. const greeting = 'hello';
    let n = 42;
    n = 5;
    const add = (x, y) => x + y;
    const printAndReturn = (string) => {
    console.log(string);
    return string;
    };
    function greet(g = 'Hi') {
    console.log(g);
    }
    function print(a, ...args) {
    console.log(a, ...args);
    }
    const [x, ...y] = ['foo', 'bar', 'baz'];
    console.log(x); // foo
    console.log(y); // ['bar', 'baz']
    function greet() {
    var g = arguments[0] === undefined ? 'Hi' : arguments[0];
    console.log(g);
    }

    View Slide

  41. const greeting = 'hello';
    let n = 42;
    n = 5;
    const add = (x, y) => x + y;
    const printAndReturn = (string) => {
    console.log(string);
    return string;
    };
    function greet(g = 'Hi') {
    console.log(g);
    }
    function print(a, ...args) {
    console.log(a, ...args);
    }
    const [x, ...y] = ['foo', 'bar', 'baz'];
    console.log(x); // foo
    console.log(y); // ['bar', 'baz']
    function print(a) {
    var args = [].slice.call(arguments, 1);
    console.log.apply(console, [a],concat(args));
    }

    View Slide

  42. const greeting = 'hello';
    let n = 42;
    n = 5;
    const add = (x, y) => x + y;
    const printAndReturn = (string) => {
    console.log(string);
    return string;
    };
    function greet(g = 'Hi') {
    console.log(g);
    }
    function print(a, ...args) {
    console.log(a, ...args);
    }
    const [x, ...y] = ['foo', 'bar', 'baz'];
    console.log(x); // foo
    console.log(y); // ['bar', 'baz']
    var _ref = ['foo', 'bar', 'baz'];
    var x = _ref[0];
    var y = _ref.slice(1);
    console.log(x);
    console.log(y);

    View Slide

  43. Declarative vs. Imperative

    View Slide

  44. Imperative
    • Most familiar style of programming
    • Telling the computer how to accomplish a task
    • Algorithms are a series of steps
    • Mutable data usually involved
    • C, C++, Java

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  50. Declarative
    • Typically associated with functional programming
    • Telling the computer what you want, not how to get it
    • Algorithms are compositions of functions
    • Immutable data (or minimal side effects) preferred
    • SQL, HTML, Haskell, DSLs

    View Slide

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

    View Slide

  52. First Class Functions
    Functions are expressions/values.

    View Slide

  53. First Class Functions
    Functions are expressions/values.
    // Function declaration
    function add(x, y) {
    return x + y;
    }
    // Assign to a variable
    const addAlias = add;
    // Assign an anonymous function expression
    const multiply = (x, y) => x * y;
    // Functions have properties
    console.log(add.name); // 'add'
    console.log(addAlias.name); // 'add'
    console.log(multiply.name); // ''

    View Slide

  54. First Class Functions
    Functions are expressions/values.
    // Function declaration
    function add(x, y) {
    return x + y;
    }
    // Assign to a variable
    const addAlias = add;
    // Assign an anonymous function expression
    const multiply = (x, y) => x * y;
    // Functions have properties
    console.log(add.name); // 'add'
    console.log(addAlias.name); // 'add'
    console.log(multiply.name); // ''

    View Slide

  55. First Class Functions
    Functions are expressions/values.
    // Function declaration
    function add(x, y) {
    return x + y;
    }
    // Assign to a variable
    const addAlias = add;
    // Assign an anonymous function expression
    const multiply = (x, y) => x * y;
    // Functions have properties
    console.log(add.name); // 'add'
    console.log(addAlias.name); // 'add'
    console.log(multiply.name); // ''

    View Slide

  56. First Class Functions
    Functions are expressions/values.
    // Function declaration
    function add(x, y) {
    return x + y;
    }
    // Assign to a variable
    const addAlias = add;
    // Assign an anonymous function expression
    const multiply = (x, y) => x * y;
    // Functions have properties
    console.log(add.name); // 'add'
    console.log(addAlias.name); // 'add'
    console.log(multiply.name); // ''

    View Slide

  57. First Class Functions
    Functions as arguments

    View Slide

  58. First Class Functions
    Functions as arguments
    // Passing in anonymous functions
    myEventLib.on('update', (data) => console.log(data));
    function doubleNumber(n) {
    return n * 2;
    }
    // Earlier example, passing in named function
    function doubleNumbers(numbers) {
    return numbers.map(doubleNumber);
    }

    View Slide

  59. First Class Functions
    Functions as arguments
    // Passing in anonymous functions
    myEventLib.on('update', (data) => console.log(data));
    function doubleNumber(n) {
    return n * 2;
    }
    // Earlier example, passing in named function
    function doubleNumbers(numbers) {
    return numbers.map(doubleNumber);
    }

    View Slide

  60. First Class Functions
    Functions as return values

    View Slide

  61. First Class Functions
    Functions as return values
    // Return a new function that adds a number
    // to another
    function additionFactory(x) {
    return (y) => x + y;
    }
    const add1 = additionFactory(1);
    add1(2); // 3

    View Slide

  62. First Class Functions
    Functions as return values
    // Return a new function that adds a number
    // to another
    function additionFactory(x) {
    return (y) => x + y;
    }
    const add1 = additionFactory(1);
    add1(2); // 3

    View Slide

  63. First Class Functions
    Functions as return values
    // Return a new function that adds a number
    // to another
    function additionFactory(x) {
    return (y) => x + y;
    }
    const add1 = additionFactory(1);
    add1(2); // 3

    View Slide

  64. Closures

    View Slide

  65. Closures
    • Allow functions to reference other scopes
    • “Close over” variables in higher scopes
    • Critical to functional programming paradigms like partial
    application

    View Slide

  66. Closures
    // New function “closes” over x
    function additionFactory(x) {
    return (y) => x + y;
    }
    const add1 = additionFactory(1);
    add1(2); // 3

    View Slide

  67. // New function “closes” over x
    function additionFactory(x) {
    return (y) => x + y;
    }
    const add1 = additionFactory(1);
    add1(2); // 3
    Closures
    Same x

    View Slide

  68. Closures
    let myValue = 'foo';
    function closure() {
    console.log(myValue);
    }
    function trickyClosure() {
    let myValue = 'bar';
    console.log(myValue);
    }
    function anotherTrickyClosure() {
    myValue = 'hello world';
    console.log(myValue);
    }
    closure(); // 'foo'
    trickyClosure(); // 'bar'
    closure(); // 'foo'
    anotherTrickyClosure(); // 'hello world'
    closure(); // 'hello world'
    trickyClosure(); // 'bar'

    View Slide

  69. Closures
    let myValue = 'foo';
    function closure() {
    console.log(myValue);
    }
    function trickyClosure() {
    let myValue = 'bar';
    console.log(myValue);
    }
    function anotherTrickyClosure() {
    myValue = 'hello world';
    console.log(myValue);
    }
    closure(); // 'foo'
    trickyClosure(); // 'bar'
    closure(); // 'foo'
    anotherTrickyClosure(); // 'hello world'
    closure(); // 'hello world'
    trickyClosure(); // 'bar'

    View Slide

  70. let myValue = 'foo';
    function closure() {
    console.log(myValue);
    }
    function trickyClosure() {
    let myValue = 'bar';
    console.log(myValue);
    }
    function anotherTrickyClosure() {
    myValue = 'hello world';
    console.log(myValue);
    }
    closure(); // 'foo'
    trickyClosure(); // 'bar'
    closure(); // 'foo'
    anotherTrickyClosure(); // 'hello world'
    closure(); // 'hello world'
    trickyClosure(); // 'bar'
    Closures

    View Slide

  71. Closures
    let myValue = 'foo';
    function closure() {
    console.log(myValue);
    }
    function trickyClosure() {
    let myValue = 'bar';
    console.log(myValue);
    }
    function anotherTrickyClosure() {
    myValue = 'hello world';
    console.log(myValue);
    }
    closure(); // 'foo'
    trickyClosure(); // 'bar'
    closure(); // 'foo'
    anotherTrickyClosure(); // 'hello world'
    closure(); // 'hello world'
    trickyClosure(); // 'bar'

    View Slide

  72. Closures
    let myValue = 'foo'; // Becomes 'hello world'
    function closure() {
    console.log(myValue);
    }
    function trickyClosure() {
    let myValue = 'bar';
    console.log(myValue);
    }
    function anotherTrickyClosure() {
    myValue = 'hello world';
    console.log(myValue);
    }
    closure(); // 'foo'
    trickyClosure(); // 'bar'
    closure(); // 'foo'
    anotherTrickyClosure(); // 'hello world'
    closure(); // 'hello world'
    trickyClosure(); // 'bar'

    View Slide

  73. Closures
    let myValue = 'foo'; // Now 'hello world'
    function closure() {
    console.log(myValue);
    }
    function trickyClosure() {
    let myValue = 'bar';
    console.log(myValue);
    }
    function anotherTrickyClosure() {
    myValue = 'hello world';
    console.log(myValue);
    }
    closure(); // 'foo'
    trickyClosure(); // 'bar'
    closure(); // 'foo'
    anotherTrickyClosure(); // 'hello world'
    closure(); // 'hello world'
    trickyClosure(); // 'bar'

    View Slide

  74. Closures
    let myValue = 'foo'; // Now 'hello world'
    function closure() {
    console.log(myValue);
    }
    function trickyClosure() {
    let myValue = 'bar';
    console.log(myValue);
    }
    function anotherTrickyClosure() {
    myValue = 'hello world';
    console.log(myValue);
    }
    closure(); // 'foo'
    trickyClosure(); // 'bar'
    closure(); // 'foo'
    anotherTrickyClosure(); // 'hello world'
    closure(); // 'hello world'
    trickyClosure(); // 'bar'

    View Slide

  75. Referential Transparency

    View Slide

  76. Referential Transparency
    • Fuzzy term associated with purity
    • For a given input, a function always returns the same value
    • Replace function calls with return value
    • No dependency on state outside function

    View Slide

  77. Referential
    Transparency
    function doubleNumber(n) {
    return n * 2;
    }
    function square(n) {
    return n * n;
    }
    function add6(n) {
    return n + 6;
    }
    // Reduces down to the same value
    let value1 = add6(square(doubleNumber(3))); // 42
    let value2 = add6(square(6)); // 42
    let value3 = add6(36); // 42
    let value4 = 42;
    // Same value for every call
    let otherValue1 = doubleNumber(3) + doubleNumber(3) +
    doubleNumber(3) + doubleNumber(3);
    let otherValue2 = 6 + 6 + 6 + 6;

    View Slide

  78. Referential
    Transparency
    function doubleNumber(n) {
    return n * 2;
    }
    function square(n) {
    return n * n;
    }
    function add6(n) {
    return n + 6;
    }
    // Reduces down to the same value
    let value1 = add6(square(doubleNumber(3))); // 42
    let value2 = add6(square(6)); // 42
    let value3 = add6(36); // 42
    let value4 = 42;
    // Same value for every call
    let otherValue1 = doubleNumber(3) + doubleNumber(3) +
    doubleNumber(3) + doubleNumber(3);
    let otherValue2 = 6 + 6 + 6 + 6;

    View Slide

  79. Referential
    Transparency
    function doubleNumber(n) {
    return n * 2;
    }
    function square(n) {
    return n * n;
    }
    function add6(n) {
    return n + 6;
    }
    // Reduces down to the same value
    let value1 = add6(square(doubleNumber(3))); // 42
    let value2 = add6(square(6)); // 42
    let value3 = add6(36); // 42
    let value4 = 42;
    // Same value for every call
    let otherValue1 = doubleNumber(3) + doubleNumber(3) +
    doubleNumber(3) + doubleNumber(3);
    let otherValue2 = 6 + 6 + 6 + 6;

    View Slide

  80. Referential
    Transparency
    let counter = 0;
    function addToCounter1(n) {
    counter++;
    return n + counter;
    }
    function addToCounter2(n) {
    let counter = 0;
    counter++;
    return n + counter;
    }
    // Not referentially transparent
    let value1 = addToCounter1(1) + addToCounter1(1); // 5
    let value2 = 2 + 3; // Not the same value every call!
    // Referentially transparent
    let otherValue1 = addToCounter2(1) + addToCounter2(1); // 4
    let otherValue2 = 2 + 2;

    View Slide

  81. Referential
    Transparency
    let counter = 0;
    function addToCounter1(n) {
    counter++;
    return n + counter;
    }
    function addToCounter2(n) {
    let counter = 0;
    counter++;
    return n + counter;
    }
    // Not referentially transparent
    let value1 = addToCounter1(1) + addToCounter1(1); // 5
    let value2 = 2 + 3; // Not the same value every call!
    // Referentially transparent
    let otherValue1 = addToCounter2(1) + addToCounter2(1); // 4
    let otherValue2 = 2 + 2;

    View Slide

  82. Referential
    Transparency
    let counter = 0;
    function addToCounter1(n) {
    counter++;
    return n + counter;
    }
    function addToCounter2(n) {
    let counter = 0;
    counter++;
    return n + counter;
    }
    // Not referentially transparent
    let value1 = addToCounter1(1) + addToCounter1(1); // 5
    let value2 = 2 + 3; // Not the same value every call!
    // Referentially transparent
    let otherValue1 = addToCounter2(1) + addToCounter2(1); // 4
    let otherValue2 = 2 + 2;

    View Slide

  83. Idempotency

    View Slide

  84. Idempotency
    • Special case of referential transparency
    • Multiple function applications produce the same result as
    one application

    View Slide

  85. Idempotency
    let abs = Math.abs;
    function identity(x) {
    return x;
    }
    function add2(n) {
    return n + 2;
    }
    // Idempotent and referentially transparent
    abs(abs(abs(abs(-1)))) === abs(-1);
    identity(identity(identity(42))) === identity(42);
    // Not idempotent, but referentially transparent
    add2(add2(add2(1))) !== add2(1); // 7 !== 3

    View Slide

  86. Idempotency
    let abs = Math.abs;
    function identity(x) {
    return x;
    }
    function add2(n) {
    return n + 2;
    }
    // Idempotent and referentially transparent
    abs(abs(abs(abs(-1)))) === abs(-1);
    identity(identity(identity(42))) === identity(42);
    // Not idempotent, but referentially transparent
    add2(add2(add2(1))) !== add2(1); // 7 !== 3

    View Slide

  87. Idempotency
    let abs = Math.abs;
    function identity(x) {
    return x;
    }
    function add2(n) {
    return n + 2;
    }
    // Idempotent and referentially transparent
    abs(abs(abs(abs(-1)))) === abs(-1);
    identity(identity(identity(42))) === identity(42);
    // Not idempotent, but referentially transparent
    add2(add2(add2(1))) !== add2(1); // 7 !== 3

    View Slide

  88. Purity

    View Slide

  89. Purity
    • “Strict referential transparency”
    • No side effects
    • No global/shared state
    • No impure arguments
    • No I/O

    View Slide

  90. Pure Functions
    function add(x, y) {
    return x + y;
    }
    function capitalize(string) {
    return string[0].toUpperCase() +
    string.slice(1).toLowerCase();
    }
    function toTitleCase(string) {
    return string
    .split(/\s+/)
    .map(capitalize)
    .join(' ');
    }

    View Slide

  91. Pure Functions
    function add(x, y) {
    return x + y;
    }
    function capitalize(string) {
    return string[0].toUpperCase() +
    string.slice(1).toLowerCase();
    }
    function toTitleCase(string) {
    return string
    .split(/\s+/)
    .map(capitalize)
    .join(' ');
    }

    View Slide

  92. Pure Functions
    function add(x, y) {
    return x + y;
    }
    function capitalize(string) {
    return string[0].toUpperCase() +
    string.slice(1).toLowerCase();
    }
    function toTitleCase(string) {
    return string
    .split(/\s+/)
    .map(capitalize)
    .join(' ');
    }

    View Slide

  93. Pure Functions
    function add(x, y) {
    return x + y;
    }
    function capitalize(string) {
    return string[0].toUpperCase() +
    string.slice(1).toLowerCase();
    }
    function toTitleCase(string) {
    return string
    .split(/\s+/)
    .map(capitalize)
    .join(' ');
    }

    View Slide

  94. Pure Functions
    function add(x, y) {
    return x + y;
    }
    function capitalize(string) {
    return string[0].toUpperCase() +
    string.slice(1).toLowerCase();
    }
    function toTitleCase(string) {
    return string
    .split(/\s+/)
    .map(capitalize)
    .join(' ');
    }
    • No side effects.
    • String and number
    arguments are immutable.

    View Slide

  95. Impure Functions
    let myName = 'Jeremy';
    const myHobbies = ['programming', 'reading', 'playing guitar'];
    function getName() {
    return myName;
    }
    function printName(name) {
    console.log(name);
    }
    function add(x, y) {
    myName = 'Joe';
    return x + y;
    }
    function hobbiesMapper(hobbies) {
    return (fn) => hobbies.map(fn);
    }

    View Slide

  96. let myName = 'Jeremy';
    const myHobbies = ['programming', 'reading', 'playing guitar'];
    function getName() {
    return myName;
    }
    function printName(name) {
    console.log(name);
    }
    function add(x, y) {
    myName = 'Joe';
    return x + y;
    }
    function hobbiesMapper(hobbies) {
    return (fn) => hobbies.map(fn);
    }
    Impure Functions
    Accesses global state

    View Slide

  97. let myName = 'Jeremy';
    const myHobbies = ['programming', 'reading', 'playing guitar'];
    function getName() {
    return myName;
    }
    function printName(name) {
    console.log(name);
    }
    function add(x, y) {
    myName = 'Joe';
    return x + y;
    }
    function hobbiesMapper(hobbies) {
    return (fn) => hobbies.map(fn);
    }
    Impure Functions
    Prints to stdout

    View Slide

  98. let myName = 'Jeremy';
    const myHobbies = ['programming', 'reading', 'playing guitar'];
    function getName() {
    return myName;
    }
    function printName(name) {
    console.log(name);
    }
    function add(x, y) {
    myName = 'Joe';
    return x + y;
    }
    function hobbiesMapper(hobbies) {
    return (fn) => hobbies.map(fn);
    }
    Impure Functions
    Modifies global state

    View Slide

  99. let myName = 'Jeremy';
    const myHobbies = ['programming', 'reading', 'playing guitar'];
    function getName() {
    return myName;
    }
    function printName(name) {
    console.log(name);
    }
    function add(x, y) {
    myName = 'Joe';
    return x + y;
    }
    function hobbiesMapper(hobbies) {
    return (fn) => hobbies.map(fn);
    }
    Impure Functions
    Array may mutate

    View Slide

  100. Recursion

    View Slide

  101. Recursion
    • Defining a solution to a problem in terms of itself
    • Solve the problem by solving smaller pieces
    • Similar to inductive proofs
    • In programming, a function that calls itself
    • In FP, imperative looping (e.g. with while or for) is
    practically nonexistent, so solve with recursion

    View Slide

  102. Recursion: Factorial

    View Slide

  103. Recursion: Factorial
    function factorial(n) {
    let result = 1;
    while (n > 1) {
    result *= n--;
    }
    return result;
    }
    Imperative

    View Slide

  104. Recursion: Factorial
    function factorial(n) {
    let result = 1;
    while (n > 1) {
    result *= n--;
    }
    return result;
    }
    Imperative
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }
    Recursive

    View Slide

  105. Recursion: Factorial
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }

    View Slide

  106. Recursion: Factorial
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }
    Base case

    View Slide

  107. Recursion: Factorial
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }
    Recursive call

    View Slide

  108. Recursion: Factorial

    View Slide

  109. Recursion: Factorial
    factorial(4);

    View Slide

  110. Recursion: Factorial
    factorial(4);
    4 * factorial(3);

    View Slide

  111. Recursion: Factorial
    factorial(4);
    4 * factorial(3);
    4 * 3 * factorial(2);

    View Slide

  112. Recursion: Factorial
    factorial(4);
    4 * factorial(3);
    4 * 3 * factorial(2);
    4 * 3 * 2 * factorial(1);

    View Slide

  113. Recursion: Factorial
    factorial(4);
    4 * factorial(3);
    4 * 3 * factorial(2);
    4 * 3 * 2 * factorial(1);
    4 * 3 * 2 * 1;

    View Slide

  114. Recursion: Factorial
    factorial(4);
    4 * factorial(3);
    4 * 3 * factorial(2);
    4 * 3 * 2 * factorial(1);
    4 * 3 * 2 * 1;
    4 * 3 * 2;

    View Slide

  115. Recursion: Factorial
    factorial(4);
    4 * factorial(3);
    4 * 3 * factorial(2);
    4 * 3 * 2 * factorial(1);
    4 * 3 * 2 * 1;
    4 * 3 * 2;
    4 * 6;

    View Slide

  116. Recursion: Factorial
    factorial(4);
    4 * factorial(3);
    4 * 3 * factorial(2);
    4 * 3 * 2 * factorial(1);
    4 * 3 * 2 * 1;
    4 * 3 * 2;
    4 * 6;
    24;

    View Slide

  117. What about recursion
    performance?

    View Slide

  118. Recursion Performance
    let value = factorial(100000);
    console.log(value); // ???

    View Slide

  119. Recursion Performance
    let value = factorial(100000);
    console.log(value); // ???
    RangeError: Maximum call
    stack size exceeded

    View Slide

  120. Recursion: Factorial
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }

    View Slide

  121. Recursion: Factorial
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }

    View Slide

  122. Recursion: Factorial
    factorial(4);
    4 * factorial(3);
    4 * 3 * factorial(2);
    4 * 3 * 2 * factorial(1);
    4 * 3 * 2 * 1;
    4 * 3 * 2;
    4 * 6;
    24;

    View Slide

  123. Recursion: Factorial
    factorial(4);
    4 * factorial(3);
    4 * 3 * factorial(2);
    4 * 3 * 2 * factorial(1);
    4 * 3 * 2 * 1;
    4 * 3 * 2;
    4 * 6;
    24;

    Stack

    View Slide

  124. Recursion: Factorial
    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)
    Stack

    View Slide

  125. Recursion: Factorial

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

    View Slide

  126. Recursion: Factorial

    factorial(4)
    factorial(3)
    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(2)
    Stack

    View Slide

  127. Recursion: Factorial

    factorial(4)
    factorial(3)
    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(2)
    factorial(1)
    Stack

    View Slide

  128. Recursion: Factorial

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

    View Slide

  129. Recursion: Factorial

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

    View Slide

  130. Recursion: Factorial

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

    View Slide

  131. Recursion: Factorial

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

    View Slide

  132. Recursion Performance
    let value = factorial(100000);
    console.log(value); // ???
    100,000 calls = 100,000 stack frames
    1 stack frame ≈ 48b
    Max stack usage ≈ 1mb
    100,000 × 48 / 1024 / 1024 = 4.58mb > 1mb

    View Slide

  133. Tail call optimization to the
    rescue!

    View Slide

  134. Tail Call Optimization

    View Slide

  135. Tail Call Optimization
    • Optimize recursive functions to not exhaust max stack
    size
    • Replace stack frames instead of adding new ones
    • The last statement before returning must be recursive call
    • Typically rewrite function to include an accumulator that
    holds the current result
    • (Coming to JavaScript in ES2015!)

    View Slide

  136. Tail Call Optimization
    Unoptimizable
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }

    View Slide

  137. Tail Call Optimization
    Unoptimizable
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }
    1

    View Slide

  138. Tail Call Optimization
    Unoptimizable
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }
    1
    2

    View Slide

  139. Tail Call Optimization
    Unoptimizable
    function factorial(n) {
    if (n < 2) {
    return 1;
    }
    return n * factorial(n - 1);
    }
    1
    3
    2

    View Slide

  140. Optimize Factorial
    function factorial(n, accum = 1) {
    if (n < 2) {
    return accum;
    }
    return factorial(n - 1, n * accum);
    }

    View Slide

  141. Optimize Factorial
    function factorial(n, accum = 1) {
    if (n < 2) {
    return accum;
    }
    return factorial(n - 1, n * accum);
    }
    Current value
    accumulator

    View Slide

  142. Optimize Factorial
    function factorial(n, accum = 1) {
    if (n < 2) {
    return accum;
    }
    return factorial(n - 1, n * accum);
    }
    Return the accumulator
    for base case

    View Slide

  143. Optimize Factorial
    function factorial(n, accum = 1) {
    if (n < 2) {
    return accum;
    }
    return factorial(n - 1, n * accum);
    }
    Move calculation
    inside the call

    View Slide

  144. Optimize Factorial
    function factorial(n, accum = 1) {
    if (n < 2) {
    return accum;
    }
    return factorial(n - 1, n * accum);
    }
    1

    View Slide

  145. Optimize Factorial
    function factorial(n, accum = 1) {
    if (n < 2) {
    return accum;
    }
    return factorial(n - 1, n * accum);
    }
    1 2

    View Slide

  146. Optimize Factorial
    function factorial(n, accum = 1) {
    if (n < 2) {
    return accum;
    }
    return factorial(n - 1, n * accum);
    }
    1
    3
    2

    View Slide

  147. And now?
    let value = factorial(100000);
    console.log(value);
    // Infinity

    View Slide

  148. Factorial TCO
    factorial(4 /*, 1 */);
    factorial(3, 4);
    factorial(2, 12);
    factorial(1, 24);
    24;

    View Slide

  149. Factorial TCO
    factorial(4 /*, 1 */);
    factorial(3, 4);
    factorial(2, 12);
    factorial(1, 24);
    24;

    Stack

    View Slide

  150. Factorial TCO

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

    View Slide

  151. Factorial TCO

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

    View Slide

  152. Factorial TCO

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

    View Slide

  153. Factorial TCO

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

    View Slide

  154. Factorial TCO

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

    View Slide

  155. Recursion: Fibonacci

    View Slide

  156. Recursion: Fibonacci

    View Slide

  157. Recursion: Fibonacci
    Base
    cases

    View Slide

  158. Recursion: Fibonacci
    Recurrence

    View Slide

  159. Recursion: Fibonacci
    function fibonacci(n) {
    if (n < 2) {
    return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
    }

    View Slide

  160. Recursion: Fibonacci
    function fibonacci(n) {
    if (n < 2) {
    return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
    }
    Base
    Cases

    View Slide

  161. Recursion: Fibonacci
    function fibonacci(n) {
    if (n < 2) {
    return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
    }
    Recurrence

    View Slide

  162. Recursion: Fibonacci
    fib(0); // 0
    fib(1); // 1
    fib(2); // 1
    fib(3); // 2
    fib(4); // 3
    fib(5); // 5
    function fibonacci(n) {
    if (n < 2) {
    return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
    }

    View Slide

  163. Performance Revisited
    fibonacci(1); // 0 ms

    View Slide

  164. Performance Revisited
    fibonacci(1); // 0 ms
    fibonacci(5); // 0 ms

    View Slide

  165. Performance Revisited
    fibonacci(1); // 0 ms
    fibonacci(5); // 0 ms
    fibonacci(20); // 0 ms

    View Slide

  166. Performance Revisited
    fibonacci(1); // 0 ms
    fibonacci(5); // 0 ms
    fibonacci(20); // 0 ms
    fibonacci(30); // 13 ms

    View Slide

  167. Performance Revisited
    fibonacci(1); // 0 ms
    fibonacci(5); // 0 ms
    fibonacci(20); // 0 ms
    fibonacci(30); // 13 ms
    fibonacci(35); // 131 ms

    View Slide

  168. Performance Revisited
    fibonacci(1); // 0 ms
    fibonacci(5); // 0 ms
    fibonacci(20); // 0 ms
    fibonacci(30); // 13 ms
    fibonacci(35); // 131 ms
    fibonacci(40); // 1516 ms

    View Slide

  169. Performance Revisited
    fibonacci(1); // 0 ms
    fibonacci(5); // 0 ms
    fibonacci(20); // 0 ms
    fibonacci(30); // 13 ms
    fibonacci(35); // 131 ms
    fibonacci(40); // 1516 ms
    fibonacci(45); // 16.6 seconds!

    View Slide

  170. Performance Revisited
    fibonacci(1); // 0 ms
    fibonacci(5); // 0 ms
    fibonacci(20); // 0 ms
    fibonacci(30); // 13 ms
    fibonacci(35); // 131 ms
    fibonacci(40); // 1516 ms
    fibonacci(45); // 16.6 seconds!
    fibonacci(100); // Who knows how long

    View Slide

  171. Exponential Growth
    Runtime (ms)
    0
    4500
    9000
    13500
    18000
    Nth number of Fibonacci Sequence
    0 10 20 30 40 50

    View Slide

  172. Recursive Call Tree

    View Slide

  173. Recursive Call Tree
    Computing some of the same values several times!

    View Slide

  174. Recursive Call Tree
    Computing some of the same values several times!
    fibonacci(4) x 2

    View Slide

  175. Recursive Call Tree
    Computing some of the same values several times!
    fibonacci(4) x 2
    fibonacci(3) x 3

    View Slide

  176. Recursive Call Tree
    Computing some of the same values several times!
    fibonacci(4) x 2
    fibonacci(3) x 3
    fibonacci(2) x 5

    View Slide

  177. Recursive Call Tree
    Computing some of the same values several times!
    fibonacci(4) x 2
    fibonacci(3) x 3
    fibonacci(2) x 5
    fibonacci(1) x 3

    View Slide

  178. TCO and Dynamic Programming

    View Slide

  179. TCO and Dynamic Programming
    • Build up the result from the
    leaves of the tree
    • Avoids recalculating the same
    values
    • Make recursive calls in the
    reverse direction

    View Slide

  180. Fibonacci TCO
    function fibonacci(n, current = 0, next = 1) {
    if (n === 0) {
    return current;
    }
    return fibonacci(n - 1, next, current + next);
    }

    View Slide

  181. Fibonacci TCO
    function fibonacci(n, current = 0, next = 1) {
    if (n === 0) {
    return current;
    }
    return fibonacci(n - 1, next, current + next);
    }
    Two accumulators

    View Slide

  182. Fibonacci TCO
    function fibonacci(n, current = 0, next = 1) {
    if (n === 0) {
    return current;
    }
    return fibonacci(n - 1, next, current + next);
    }
    Results for next call

    View Slide

  183. Fibonacci TCO
    function fibonacci(n, current = 0, next = 1) {
    if (n === 0) {
    return current;
    }
    return fibonacci(n - 1, next, current + next);
    }
    Reach the end, so return
    whatever was built up

    View Slide

  184. Fibonacci TCO
    fibonacci(6 /*, 0, 1 */);
    fibonacci(5, 1, 1);
    fibonacci(4, 1, 2);
    fibonacci(3, 2, 3);
    fibonacci(2, 3, 5);
    fibonacci(1, 5, 8);
    fibonacci(0, 8, 13);
    8;

    View Slide

  185. Fibonacci TCO
    fibonacci(6 /*, 0, 1 */);
    fibonacci(5, 1, 1);
    fibonacci(4, 1, 2);
    fibonacci(3, 2, 3);
    fibonacci(2, 3, 5);
    fibonacci(1, 5, 8);
    fibonacci(0, 8, 13);
    8;
    Fibonacci
    sequence

    View Slide

  186. Fibonacci Performance
    fibonacci(1); // 0 ms
    fibonacci(5); // 0 ms
    fibonacci(20); // 0 ms
    fibonacci(30); // 0 ms
    fibonacci(35); // 0 ms
    fibonacci(40); // 0 ms
    fibonacci(45); // 0 ms
    fibonacci(100); // 0 ms

    View Slide

  187. Functional Arrays

    View Slide

  188. Functional Arrays
    • Common operations like map can be implemented in FP
    • Enforce immutability by returning new array
    • No looping; use recursion!

    View Slide

  189. Array#map
    Map values in array to other values in new array
    function map(fn, array) {
    if (array.length === 0) {
    return array;
    }
    const [head, ...tail] = array;
    return [fn(head), ...map(fn, tail)];
    }

    View Slide

  190. Array#map
    Map values in array to other values in new array
    function map(fn, array) {
    if (array.length === 0) {
    return array;
    }
    const [head, ...tail] = array;
    return [fn(head), ...map(fn, tail)];
    }
    Function first; good
    for currying

    View Slide

  191. Array#map
    Map values in array to other values in new array
    function map(fn, array) {
    if (array.length === 0) {
    return array;
    }
    const [head, ...tail] = array;
    return [fn(head), ...map(fn, tail)];
    }
    Grab the first element
    and remaining elements

    View Slide

  192. function map(fn, array) {
    if (array.length === 0) {
    return array;
    }
    const [head, ...tail] = array;
    return [fn(head), ...map(fn, tail)];
    }
    Array#map
    Map values in array to other values in new array
    var head = array[0];

    View Slide

  193. function map(fn, array) {
    if (array.length === 0) {
    return array;
    }
    const [head, ...tail] = array;
    return [fn(head), ...map(fn, tail)];
    }
    Array#map
    Map values in array to other values in new array
    var tail = array.slice(1);

    View Slide

  194. Array#map
    Map values in array to other values in new array
    function map(fn, array) {
    if (array.length === 0) {
    return array;
    }
    const [head, ...tail] = array;
    return [fn(head), ...map(fn, tail)];
    }
    Return new array with
    altered head and mapped
    array of remaining elements

    View Slide

  195. Array#map
    Map values in array to other values in new array
    function map(fn, array) {
    if (array.length === 0) {
    return array;
    }
    const [head, ...tail] = array;
    return [fn(head), ...map(fn, tail)];
    }
    return [fn(head)].concat(map(fn, tail));

    View Slide

  196. Array#map
    Map values in array to other values in new array
    function map(fn, array) {
    if (array.length === 0) {
    return array;
    }
    const [head, ...tail] = array;
    return [fn(head), ...map(fn, tail)];
    }
    Base case: empty array.

    View Slide

  197. Array#map
    Map values in array to other values in new array
    function square(n) {
    return n * n;
    }
    let numbers = [1, 2, 3];
    let squaredNumbers = map(square, numbers);
    // [1, 4, 9];

    View Slide

  198. Partial Application

    View Slide

  199. Partial Application
    • Assign values to function parameters without evaluating
    the function (i.e. “prefill” argument values)
    • Produce a new function that takes in any remaining
    unassigned arguments

    View Slide

  200. Partial
    Application
    // Before we would use this
    function additionFactory(x) {
    return (y) => x + y;
    }
    let add1 = additionFactory(1);
    add1(2); // 3
    // Now we can write an add function
    // and use partial application
    function add(x, y) {
    return x + y;
    }
    // Partial application with native `bind` function
    add1 = add.bind(null, 1);
    add1(2); // 3
    // Use a custom-written `partial` function
    add1 = partial(add, 1);
    add1(2); // 3

    View Slide

  201. Partial
    Application
    // Before we would use this
    function additionFactory(x) {
    return (y) => x + y;
    }
    let add1 = additionFactory(1);
    add1(2); // 3
    // Now we can write an add function
    // and use partial application
    function add(x, y) {
    return x + y;
    }
    // Partial application with native `bind` function
    add1 = add.bind(null, 1);
    add1(2); // 3
    // Use a custom-written `partial` function
    add1 = partial(add, 1);
    add1(2); // 3

    View Slide

  202. Partial
    Application
    // Before we would use this
    function additionFactory(x) {
    return (y) => x + y;
    }
    let add1 = additionFactory(1);
    add1(2); // 3
    // Now we can write an add function
    // and use partial application
    function add(x, y) {
    return x + y;
    }
    // Partial application with native `bind` function
    add1 = add.bind(null, 1);
    add1(2); // 3
    // Use a custom-written `partial` function
    add1 = partial(add, 1);
    add1(2); // 3

    View Slide

  203. Partial
    Application
    // Before we would use this
    function additionFactory(x) {
    return (y) => x + y;
    }
    let add1 = additionFactory(1);
    add1(2); // 3
    // Now we can write an add function
    // and use partial application
    function add(x, y) {
    return x + y;
    }
    // Partial application with native `bind` function
    add1 = add.bind(null, 1);
    add1(2); // 3
    // Use a custom-written `partial` function
    add1 = partial(add, 1);
    add1(2); // 3

    View Slide

  204. Partial Application
    function partial(fn, ...args) {
    return (...otherArgs) => {
    return fn(...args, ...otherArgs);
    };
    }

    View Slide

  205. Partial Application
    function partial(fn, ...args) {
    return (...otherArgs) => {
    return fn(...args, ...otherArgs);
    };
    }
    Applied arguments

    View Slide

  206. Partial Application
    function partial(fn, ...args) {
    return (...otherArgs) => {
    return fn(...args, ...otherArgs);
    };
    }
    Return new closure

    View Slide

  207. Partial Application
    function partial(fn, ...args) {
    return (...otherArgs) => {
    return fn(...args, ...otherArgs);
    };
    }
    Remaining arguments

    View Slide

  208. Partial Application
    function partial(fn, ...args) {
    return (...otherArgs) => {
    return fn(...args, ...otherArgs);
    };
    }
    Call original function
    with all arguments

    View Slide

  209. Partial Application: Why?
    • Build up functions from smaller pieces
    • Remove duplication
    • Generalize function abstraction (i.e. no
    additionFactory type functions)

    View Slide

  210. Partial Application: Why?
    function multiply(x, y) {
    return x * y;
    }
    const doubleNumber = partial(multiply, 2);
    const numbers = [1, 2, 3];
    const doubledNumbers = map(doubleNumber, numbers);
    console.log(doubledNumbers);
    // [2, 4, 6]

    View Slide

  211. Currying

    View Slide

  212. Currying
    • Similar to partial application
    • Bakes partial application into a function
    • Successive invocations of function with arguments returns
    a new function with those arguments assigned
    • Keep returning a new function until all arguments “filled,”
    and then invoke the actual function with all of those
    arguments (sounds recursive, huh?)

    View Slide

  213. Currying
    const add = curry((x, y, z) => x + y + z);
    // All arguments supplied, normal invocation.
    add(1, 2, 3); // 6
    // Supply arguments one at a time. Final argument
    // induces invocation.
    add(1)(2)(3); // 6
    // Equivalent to previous example.
    let add1 = add(1);
    let add3 = add1(2);
    add3(3); // 6
    // Supply more than one argument at a time.
    add(1, 2)(3); // 6
    add(1)(2, 3); // 6

    View Slide

  214. Currying
    const add = curry((x, y, z) => x + y + z);
    // All arguments supplied, normal invocation.
    add(1, 2, 3); // 6
    // Supply arguments one at a time. Final argument
    // induces invocation.
    add(1)(2)(3); // 6
    // Equivalent to previous example.
    let add1 = add(1);
    let add3 = add1(2);
    add3(3); // 6
    // Supply more than one argument at a time.
    add(1, 2)(3); // 6
    add(1)(2, 3); // 6

    View Slide

  215. Currying
    const add = curry((x, y, z) => x + y + z);
    // All arguments supplied, normal invocation.
    add(1, 2, 3); // 6
    // Supply arguments one at a time. Final argument
    // induces invocation.
    add(1)(2)(3); // 6
    // Equivalent to previous example.
    let add1 = add(1);
    let add3 = add1(2);
    add3(3); // 6
    // Supply more than one argument at a time.
    add(1, 2)(3); // 6
    add(1)(2, 3); // 6

    View Slide

  216. Currying
    const add = curry((x, y, z) => x + y + z);
    // All arguments supplied, normal invocation.
    add(1, 2, 3); // 6
    // Supply arguments one at a time. Final argument
    // induces invocation.
    add(1)(2)(3); // 6
    // Equivalent to previous example.
    let add1 = add(1);
    let add3 = add1(2);
    add3(3); // 6
    // Supply more than one argument at a time.
    add(1, 2)(3); // 6
    add(1)(2, 3); // 6

    View Slide

  217. Currying
    const add = curry((x, y, z) => x + y + z);
    // All arguments supplied, normal invocation.
    add(1, 2, 3); // 6
    // Supply arguments one at a time. Final argument
    // induces invocation.
    add(1)(2)(3); // 6
    // Equivalent to previous example.
    let add1 = add(1);
    let add3 = add1(2);
    add3(3); // 6
    // Supply more than one argument at a time.
    add(1, 2)(3); // 6
    add(1)(2, 3); // 6

    View Slide

  218. Currying
    const add = curry((x, y, z) => x + y + z);
    // All arguments supplied, normal invocation.
    add(1, 2, 3); // 6
    // Supply arguments one at a time. Final argument
    // induces invocation.
    add(1)(2)(3); // 6
    // Equivalent to previous example.
    let add1 = add(1);
    let add3 = add1(2);
    add3(3); // 6
    // Supply more than one argument at a time.
    add(1, 2)(3); // 6
    add(1)(2, 3); // 6

    View Slide

  219. Currying
    function curry(fn, len = fn.length) {
    return (...args) => {
    if (args.length >= len) {
    return fn(...args);
    }
    return curry(
    partial(fn, ...args),
    len - args.length
    );
    };
    }

    View Slide

  220. Currying
    function curry(fn, len = fn.length) {
    return (...args) => {
    if (args.length >= len) {
    return fn(...args);
    }
    return curry(
    partial(fn, ...args),
    len - args.length
    );
    };
    }
    Knowing arity is
    important!

    View Slide

  221. Currying
    function curry(fn, len = fn.length) {
    return (...args) => {
    if (args.length >= len) {
    return fn(...args);
    }
    return curry(
    partial(fn, ...args),
    len - args.length
    );
    };
    }
    Arguments to apply
    (or invoke)

    View Slide

  222. Currying
    function curry(fn, len = fn.length) {
    return (...args) => {
    if (args.length >= len) {
    return fn(...args);
    }
    return curry(
    partial(fn, ...args),
    len - args.length
    );
    };
    }
    Recursive call if not
    all arguments supplied

    View Slide

  223. Currying
    function curry(fn, len = fn.length) {
    return (...args) => {
    if (args.length >= len) {
    return fn(...args);
    }
    return curry(
    partial(fn, ...args),
    len - args.length
    );
    };
    }
    Curry the partially
    applied function

    View Slide

  224. Currying
    function curry(fn, len = fn.length) {
    return (...args) => {
    if (args.length >= len) {
    return fn(...args);
    }
    return curry(
    partial(fn, ...args),
    len - args.length
    );
    };
    }
    Arity decreases

    View Slide

  225. Currying
    function curry(fn, len = fn.length) {
    return (...args) => {
    if (args.length >= len) {
    return fn(...args);
    }
    return curry(
    partial(fn, ...args),
    len - args.length
    );
    };
    }
    Base case: all
    arguments
    supplied

    View Slide

  226. Currying: Why?
    let curriedMap = curry(map);
    let multiply = curry((x, y) => x * y);
    let doubleNumbers = curriedMap(multiply(2));
    let numbers = [1, 2, 3];
    console.log(doubleNumbers(numbers));
    // [2, 4, 6]

    View Slide

  227. Function Composition

    View Slide

  228. Function Composition

    View Slide

  229. Function Composition
    • Compose functions together
    to form new functions
    • Pipe function output to next
    function
    • Helps enforce modularity/
    SOC by keeping functions
    small

    View Slide

  230. Function Composition
    let multiply = curry((x, y) => x * y);
    let multiply2 = multiply(2);
    let multiply3 = multiply(3);
    let multiply6 = compose(multiply2, multiply3);
    2 * 3 * 6 === multiply6(6); // 36
    let add = curry((x, y) => x + y);
    let subtract = curry((y, x) => -y + x);
    let add5 = compose(add(6), add(1), subtract(2));
    3 - 2 + 6 + 1 === add5(3); // 8

    View Slide

  231. Function Composition
    let multiply = curry((x, y) => x * y);
    let multiply2 = multiply(2);
    let multiply3 = multiply(3);
    let multiply6 = compose(multiply2, multiply3);
    2 * 3 * 6 === multiply6(6); // 36
    let add = curry((x, y) => x + y);
    let subtract = curry((y, x) => -y + x);
    let add5 = compose(add(6), add(1), subtract(2));
    3 - 2 + 6 + 1 === add5(3); // 8

    View Slide

  232. Function Composition
    let multiply = curry((x, y) => x * y);
    let multiply2 = multiply(2);
    let multiply3 = multiply(3);
    let multiply6 = compose(multiply2, multiply3);
    2 * 3 * 6 === multiply6(6); // 36
    let add = curry((x, y) => x + y);
    let subtract = curry((y, x) => -y + x);
    let add5 = compose(add(6), add(1), subtract(2));
    3 - 2 + 6 + 1 === add5(3); // 8

    View Slide

  233. Function Composition
    function compose(...fns) {
    return (...args) => {
    const result = fns.reduceRight((memo, fn) => {
    return [fn(...memo)];
    }, args);
    return result[0];
    };
    }
    function compose(f, g) {
    return (...args) => f(g(...args));
    }

    View Slide

  234. Function Composition
    function compose(...fns) {
    return (...args) => {
    const result = fns.reduceRight((memo, fn) => {
    return [fn(...memo)];
    }, args);
    return result[0];
    };
    }
    function compose(f, g) {
    return (...args) => f(g(...args));
    }

    View Slide

  235. Function Composition
    function compose(...fns) {
    return (...args) => {
    const result = fns.reduceRight((memo, fn) => {
    return [fn(...memo)];
    }, args);
    return result[0];
    };
    }
    function compose(f, g) {
    return (...args) => f(g(...args));
    }

    View Slide

  236. Function Composition
    function compose(...fns) {
    return (...args) => {
    const result = fns.reduceRight((memo, fn) => {
    return [fn(...memo)];
    }, args);
    return result[0];
    };
    }
    function compose(f, g) {
    return (...args) => f(g(...args));
    }

    View Slide

  237. Function Composition
    function compose(...fns) {
    return (...args) => {
    const result = fns.reduceRight((memo, fn) => {
    return [fn(...memo)];
    }, args);
    return result[0];
    };
    }
    function compose(f, g) {
    return (...args) => f(g(...args));
    }

    View Slide


  238. View Slide

  239. But why Functional
    Programming?

    View Slide

  240. Why FP
    • Concise, elegant solutions to problems
    • Modularity and composition
    • Eliminate data races with immutability
    • Unit tests are simpler — no checking for state changes

    View Slide

  241. Extra Resources
    • Babel (babeljs.io)
    • Lodash (lodash.com)
    • Clojurescript (github.com/clojure/clojurescript)
    • Immutable.js (github.com/facebook/immutable-js)
    • React (facebook.github.io/react/)

    View Slide

  242. Thanks!
    jfairbank
    @elpapapollo
    blog.jeremyfairbank.com
    Code samples (some ES5 too)
    github.com/jfairbank/functional-javascript

    View Slide