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

A Humane Introduction to Functional Programming

A Humane Introduction to Functional Programming

A gentle look at what functional programming is.

John S.

May 09, 2014
Tweet

More Decks by John S.

Other Decks in Programming

Transcript

  1. Functional Programming “… treats computation as the evaluation of mathematical

    functions and avoids state and mutable data #sjsJohn
  2. Humane “Characterised by tenderness, compassion, and sympathy for people and

    animals, especially for the suffering or distressed #sjsJohn
  3. #sjsJohn var makeAdder = function (x) { return function (n)

    { return x + n; }; }; ! _.map([2, 3, 5], makeAdder(7)); // [9, 10, 12] 1 2 3 4 5 6 7
  4. #sjsJohn var map = function (list, func) { var i,

    result = []; ! for (i = 0; i < list.length; i++) { result.push(func(list[i])); } ! return result; }; 1 2 3 4 5 6 7 8 9
  5. #sjsJohn var x = 3; ! var impure = function

    (y) { return x + y; }; ! expect(impure(4)).toBe(7); 1 2 3 4 5 6 7
  6. #sjsJohn var map = function (list, func) { var i,

    result = []; ! for (i = 0; i < list.length; i++) { result.push(func(list[i])); } ! return result; }; 1 2 3 4 5 6 7 8 9
  7. #sjsJohn var map = function (list, func) { if (list.length

    == 0) { return []; } else { return [func(list[0])].concat( map(list.slice(1), func) ); } }; 1 2 3 4 5 6 7 8 9
  8. “If a pure function mutates some local data in order

    to produce an immutable return value, is that ok? #sjsJohn -Rich Hickey
  9. Project Euler Problem No. 2 #sjsJohn By considering the terms

    in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
  10. var euler2 = function () { var a = 1,

    b = 1, result = 0, next = a + b; ! while (next <= 4000000) { if (next % 2 == 0) result += next; a = b; b = next; next = a + b; } ! return result; }; 1 2 3 4 5 6 7 8 9 10 11 12 13 #sjsJohn
  11. var euler2 = function () { var a = 1,

    b = 1, result = 0, next = a + b; ! while (next <= 4000000) { if (next % 2 == 0) result += next; a = b; b = next; next = a + b; } ! return result; }; 1 2 3 4 5 6 7 8 9 10 11 12 13 #sjsJohn
  12. var fib = function (n) { if (n < 2)

    return 1; return fib(n - 1) + fib(n - 2); }; 1 2 3 4 #sjsJohn
  13. var fibSequence = function (max) { var i, seq =

    []; ! for (i = 0; fib(i) <= max; i++) { seq.push(fib(i)); } ! return seq; }; 1 2 3 4 5 6 7 8 9 #sjsJohn
  14. var fibSequence = function (pred) { var i, seq =

    []; ! for (i = 0; pred(fib(i), i); i++) { seq.push(fib(i)); } ! return seq; }; 1 2 3 4 5 6 7 8 9 #sjsJohn
  15. var twentyTerms = function (f, i) { return i <

    20; }; ! var maxTerm = function (max) { return function (f, i) { return f <= max; }; }; 1 2 3 4 5 6 7 8 9 #sjsJohn
  16. var isEven = function (n) { return n % 2

    == 0; }; ! var fibs = _.filter( fibSequence(maxTerm(4000000)), isEven ); 1 2 3 4 5 6 7 8 #sjsJohn
  17. The Sum of a List #sjsJohn … is the first

    item plus the sum of the rest.
  18. var add = function (a, b) { return a +

    b; }; ! var sum = function (list) { return _.reduce(list, add, 0); }; 1 2 3 4 5 6 7 #sjsJohn
  19. _.reduce([1, 2, 3, 4, 5], add, 0); ! add(0, 1)

    // 1 add(1, 2) // 3 add(3, 3) // 6 add(6, 4) // 10 add(10, 5) // 15 #sjsJohn 1 2 3 4 5 6 7
  20. var fib = function (n) { if (n < 2)

    return 1; return fib(n - 1) + fib(n - 2); }; 1 2 3 4 #sjsJohn
  21. var fib = _.memoize(function (n) { if (n < 2)

    return 1; return fib(n - 1) + fib(n - 2); }); 1 2 3 4 #sjsJohn
  22. var memoize = function (func) { var cache = {};

    ! return function () { if (!_.has(cache, arguments)) { cache[arguments] = func.apply(this, arguments); } return cache[arguments]; }; }; 1 2 3 4 5 6 7 8 9 10 11 #sjsJohn
  23. var fibs = function* () { var i = 0;

    ! while (true) { yield fib(i); i++; } }; 1 2 3 4 5 6 7 8 #sjsJohn
  24. var fibGenerator = fibs(); ! fibGenerator.next(); // 1 fibGenerator.next(); //

    1 fibGenerator.next(); // 2 fibGenerator.next(); // 3 fibGenerator.next(); // 5 fibGenerator.next(); // 8 1 2 3 4 5 6 7 8 #sjsJohn
  25. Further … #sjsJohn Adventures in Functional Programming - Jim Weirich

    Enemy of the State - Phil Roberts Functional JavaScript - Michael Fogus