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

Building Functional Islands

Building Functional Islands

A talk about functional programming at the April 2016 Front End London http://frontendlondon.co.uk

Mark Jones

April 28, 2016
Tweet

Other Decks in Technology

Transcript

  1. Statements const nums = [1, 2, 3]; const doubled =

    []; for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2); }
  2. Statements const nums = [1, 2, 3]; const doubled =

    []; for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2); }
  3. Statements const nums = [1, 2, 3]; const doubled =

    []; for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2); }
  4. Statements const nums = [1, 2, 3]; const doubled =

    []; for (let i = 0; i < nums.length; i++) { doubled.push(nums[i] * 2); }
  5. function double(x) { return x * 2; } map([1, 2,

    3], double); // [(1 * 2), (2 * 2), (3 * 2)] Expressions
  6. function double(x) { return x * 2; } map([1, 2,

    3], double); // [(1 * 2), (2 * 2), (3 * 2)] // [2, 4, 6] Expressions
  7. First-Class Functions function onClick() { // I'm a first-class function

    } document.body.addEventListener('click', onClick);
  8. function onClick() { // I get called by a higher-order

    function } document.body.addEventListener('click', onClick); Higher-Order Functions
  9. Higher-Order Functions function logify(fn) { return (...args) => { console.log(args);

    return fn(...args); }; } const logifyAdd = logify(add); function add(x, y) { return x + y; }
  10. Higher-Order Functions function logify(fn) { return (...args) => { console.log(args);

    return fn(...args); }; } const logifyAdd = logify(add); function add(x, y) { return x + y; }
  11. Higher-Order Functions function logify(fn) { return (...args) => { console.log(args);

    return fn(...args); }; } const logifyAdd = logify(add); function add(x, y) { return x + y; }
  12. Pure Functions add(1, 2) + add(3, 4); // 3 +

    add(3, 4); // 3 + 7; // 10;
  13. Immutable values const nums = [1, 2, 3]; const person

    = { name: 'mark', age: 29 }; nums[0] = 2; // [2, 2, 3] person.age = 27; // { name: 'mark', age: 27 }
  14. Immutable values const nums = [1, 2, 3]; const person

    = { name: 'mark', age: 29 }; nums[0] = 2; // [2, 2, 3] person.age = 27; // { name: 'mark', age: 27 }
  15. Immutable values const nums = [1, 2, 3]; const person

    = { name: 'mark', age: 29 }; nums[0] = 2; // [2, 2, 3] person.age = 27; // { name: 'mark', age: 27 }
  16. Immutable values const nums = [1, 2, 3]; const person

    = { name: 'mark', age: 29 }; nums[0] = 2; // [2, 2, 3] person.age = 27; // { name: 'mark', age: 27 }
  17. Object.freeze const nums = Object.freeze([1, 2, 3]); const person =

    Object.freeze({ name: 'mark', age: 29 }); nums[0] = 2; // [1, 2, 3] person.age = 27; // { name: 'mark', age: 29 }
  18. Object.freeze const nums = Object.freeze([1, 2, 3]); const person =

    Object.freeze({ name: 'mark', age: 29 }); nums[0] = 2; // [1, 2, 3] person.age = 27; // { name: 'mark', age: 29 }
  19. Object.freeze const nums = Object.freeze([1, 2, 3]); const person =

    Object.freeze({ name: 'mark', age: 29 }); nums[0] = 2; // [1, 2, 3] person.age = 27; // { name: 'mark', age: 29 }
  20. Object.freeze const employee = Object.freeze({ department: 'Eng', profile: { name:

    'mark', age: 29 } }); employee.profile.age = 27; // {...{ name: 'mark', age: 27 } }
  21. Object.freeze const employee = Object.freeze({ department: 'Eng', profile: { name:

    'mark', age: 29 } }); employee.profile.age = 27; // {...{ name: 'mark', age: 27 } }
  22. Object.freeze const employee = Object.freeze({ department: 'Eng', profile: { name:

    'mark', age: 29 } }); employee.profile.age = 27; // {...{ name: 'mark', age: 27 } }
  23. deepFreeze const employee = deepFreeze({ department: 'Eng', profile: { name:

    'mark', age: 29 } }); employee.profile.age = 27; // {...{ name: 'mark', age: 29 } }
  24. Currying const add = curry((x, y) => { return x

    + y; }); const succ = add(1); succ(1); // 2
  25. Currying const add = curry((x, y) => { return x

    + y; }); const succ = add(1); succ(1); // 2
  26. Currying const add = curry((x, y) => { return x

    + y; }); const succ = add(1); succ(1); // 2
  27. Currying const devs = [ { firstName: 'mark' }, {

    firstName: 'sally' } ]; const firstNames = map(devs, (dev) => { return dev.firstName; });
  28. Currying const devs = [ { firstName: 'mark' }, {

    firstName: 'sally' } ]; const firstNames = map(devs, (dev) => { return dev.firstName; });
  29. Currying const devs = [ { firstName: 'mark' }, {

    firstName: 'sally' } ]; const firstNames = map(devs, (dev) => { return dev.firstName; });
  30. Currying const devs = [ { firstName: 'mark' }, {

    firstName: 'sally' } ]; const getFirstNames = map(prop('firstName')); const firstNames = getFirstNames(devs);
  31. Currying const devs = [ { firstName: 'mark' }, {

    firstName: 'sally' } ]; const getFirstNames = map(prop('firstName')); const firstNames = getFirstNames(devs);
  32. Currying const devs = [ { firstName: 'mark' }, {

    firstName: 'sally' } ]; const getFirstNames = map(prop('firstName')); const firstNames = getFirstNames(devs);
  33. Currying const devs = [ { firstName: 'mark' }, {

    firstName: 'sally' } ]; const getFirstNames = map(prop('firstName')); const firstNames = getFirstNames(devs);
  34. Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),

    JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
  35. Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),

    JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
  36. Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),

    JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
  37. Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),

    JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
  38. Composition const getIds = map(prop('id')); const getFilmIdsFromResponse = compose(getIds, prop('films'),

    JSON.parse); getFilmIdsFromResponse('{ "films": [...], "actors": [...] }');
  39. Further reading / watch list (free) https://drboolean.gitbooks.io/mostly-adequate-guide/content/ Professor Frisby's Mostly

    Adequate Guide to Functional Programming http://ramdajs.com/ A practical functional library for Javascript programmers https://github.com/lodash/lodash/wiki/FP-Guide lodash/fp guide https://www.youtube.com/watch?v=m3svKOdZijA Hey Underscore, You're Doing It Wrong! https://github.com/substack/deep-freeze deepFreeze
  40. Further reading / watch list (paid) https://frontendmasters.com/courses/functional-js-lite/ Functional Lite JavaScript

    https://frontendmasters.com/courses/functional-javascript/ Hardcore Functional Programming in JavaScript