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

Programação Funcional com ES6

Programação Funcional com ES6

Uma introdução a Programação Funcional, usando exemplos com ES6, para despertar o interesse através de motivações reais e claras do porquê do uso da mesma.

Matheus Lima

April 25, 2016
Tweet

More Decks by Matheus Lima

Other Decks in Programming

Transcript

  1. var daysThisMonth = function() { var date = new Date();

    var year = date.getFullYear(); var month = date.getMonth(); var start = new Date(year, month, 1); var end = new Date(year, month + 1, 1); return Math.round((end - start) / (1000 * 60 * 60 * 24)); };
  2. /* Se invocar em Março */ daysThisMonth(); // 31 /*

    Se invocar em Abril */ daysThisMonth(); // 30
  3. var daysInMonth = function(year, month) { var start = new

    Date(year, month - 1, 1); var end = new Date(year, month, 1); return Math.round((end - start) / (1000 * 60 * 60 * 24)); };
  4. /* Se invocar em Março */ daysInMonth(2016, 3); // 31

    daysInMonth(2016, 3); // 31 daysInMonth(2016, 3); // 31 /* Se invocar em Abril */ daysInMonth(2016, 4); // 30 daysInMonth(2016, 4); // 30
  5. var counter = 0; var increment = function() { counter

    = counter + 1; return counter; };
  6. var printSquares = function() { for (var i = 1;

    i <= 10; i++) { console.log(i*i); } };
  7. var printSquares = function(n) { if (n > 0) {

    printSquares(n-1); console.log(n*n); } };
  8. const add = function (x, y) { return x +

    y; }; add = 42; // Uncaught SyntaxError: unknown: Line 5: "add" is read-only
  9. var add = function (x, y) { return x +

    y; }; const add = (x, y) => x + y
  10. const add = (x, y) => x + y const

    mult = (x, y) => x * y const calculate = (fn, x, y) => fn(x, y) calculate(add, 1, 2) // 3 calculate(mult, 1, 2) // 2
  11. const students = [ {name: 'Anna', grade: 6}, {name: 'John',

    grade: 4}, {name: 'Maria', grade: 9} ] ['Anna', 'John', 'Maria']
  12. const students = [{name: 'Anna', grade: 6}, {name: 'John', grade:

    4}, {name: 'Maria', grade: 9}] const studentsByName = [] for (const student of students) studentsByName.push(student.name)
  13. const students = [{name: 'Anna', grade: 6}, {name: 'John', grade:

    4}, {name: 'Maria', grade: 9}] const studentsByName = students.map(student => student.name)
  14. const students = [{name: 'Anna', grade: 6}, {name: 'John', grade:

    4}, {name: 'Maria', grade: 9}] const byName = object => object.name const studentsByName = students.map(byName)
  15. const students = [ {name: 'Anna', grade: 6}, {name: 'John',

    grade: 4}, {name: 'Maria', grade: 9} ] [{name: 'Maria', grade: 9}]
  16. const students = [{name: 'Anna', grade: 6}, {name: 'John', grade:

    4}, {name: 'Maria', grade: 9}] const approvedStudents = [] for (const student of students) if (student.grade >= 9) approvedStudents.push(student.name)
  17. const students = [{name: 'Anna', grade: 6}, {name: 'John', grade:

    4}, {name: 'Maria', grade: 9}] const isApproved = student => student.grade >= 9 const approvedStudents = students.filter(isApproved)
  18. const students = [ {name: 'Anna', grade: 6}, {name: 'John',

    grade: 4}, {name: 'Maria', grade: 9} ] 19
  19. const students = [{name: 'Anna', grade: 6}, {name: 'John', grade:

    4}, {name: 'Maria', grade: 9}] let sum = 0 for (let student of students) sum += student.grade
  20. const students = [{name: 'Anna', grade: 6}, {name: 'John', grade:

    4}, {name: 'Maria', grade: 9}] const sum = students.reduce((acc, current) => { return acc + current.grade }, 0)
  21. const students = [{name: 'Anna', grade: 6}, {name: 'John', grade:

    4}, {name: 'Maria', grade: 9}] const sum = students.reduce((acc, current) => acc + current.grade, 0)
  22. const toUpperCase = x => x.toUpperCase() const exclaim = x

    => `${x}!!!` const scream = compose(exclaim, toUpperCase) scream("stop") // STOP!!!
  23. const compose = (f,g) => x => f(g(x)) const toUpperCase

    = x => x.toUpperCase() const exclaim = x => `${x}!` const scream = compose(exclaim, toUpperCase) scream("stop") // STOP!!!
  24. const add = (x, y) => { return x +

    y } const add = (x) => { }
  25. const add = (x, y) => { return x +

    y } const add = (x) => { return (y) => { } }
  26. const add = (x, y) => { return x +

    y } const add = (x) => { return (y) => { return x + y } }
  27. const add = (x) => { return (y) => {

    return x + y } } const add = x => y => x + y
  28. const add = x => y => x + y

    const addFive = add(5) console.log(addFive) // function (y) { return x + y; } console.log(addFive(3)) // 8
  29. Rio de Janeiro Rua São José, 90 cj. 2121 -

    Centro (21) 2240-2030 São Paulo Rua Sansão Alves dos Santos, 433 - 4º andar - Brooklin (11) 4119-0449