revisão
Object !
Object ! Prototipo de Object !
Object ! Prototipo de Object ! Object.prototype.pO = 1
Object ! Prototipo de Object ! pO = 1 Object.prototype.pO = 1
A ! a = 2 var A = function() { this.a = 2; } Object ! Prototipo de Object ! pO = 1
A ! a = 2 Prototipo de A ! Object ! Prototipo de Object ! pO = 1
A ! a = 2 Prototipo de A ! A.prototype.pA = 3 Object ! Prototipo de Object ! pO = 1
A ! a = 2 Prototipo de A ! pA = 3 A.prototype.pA = 3 Object ! Prototipo de Object ! pO = 1
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 var B = function() { this.b = 4; } B ! b = 4
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 Prototipo de B !
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 Prototipo de B ! B.prototype = new A
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A new A() ! a = 2
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A new A() ! a = 2
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 B.prototype.pB = 5
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 B.prototype.pB = 5
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x = new B()
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x = new B() x new B() ! b = 4
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x = new B() x new B() ! b = 4 Prototipo de B !
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.b x new B() ! b = 4 Prototipo de B ! ?
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.pB x new B() ! b = 4 Prototipo de B ! ?
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.a x new B() ! b = 4 Prototipo de B ! ?
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.pA x new B() ! b = 4 Prototipo de B ! ?
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
A ! a = 2 Prototipo de A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.pO x new B() ! b = 4 Prototipo de B ! ?
function f (x) { return function (y) { return x + y; }; } var a = f(5); var b = a(3); alert(b); // ????
Javascript Alexandre Gomes princípios da programação funcional com
programação imperativa 1. Bata bem todos os ingredientes ( menos o fermento) da massa no liquidificador, aproximadamente 2 a 3 minutos 2. Acrescente o fermento e bata por mais uns 15 segundos 3. Coloque em uma fôrma redonda, u n t a d a c o m m a n t e i g a e polvilhada com farinha de trigo 4. Asse por cerca de 40 minutos em forno médio (180º graus), preaquecido programação declarativa Comunique assim que estiver pronto. Não deixe o bolo queimar. Não deixe que abram o forno antes da hora.
Buscar alunos com menos de 30 anos. alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ]
menores = [] ! for (int i = 1; i <= 4; i++) { aluno = alunos[i]; if (aluno.idade < 30) { menores.add(aluno); } } ! return menores; alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ]
menores = [] ! for (int i = 1; i <= 4; i++) { aluno = alunos[i]; if (aluno.idade < 30) { menores.add(aluno); } } ! return menores; alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ] imperativa
alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ] Nome Idade 1 Rodrigo 30 2 Patricia 20 3 Marcos 33 4 Ricardo 28
Nome Idade 1 Rodrigo 30 2 Patricia 20 3 Marcos 33 4 Ricardo 28 SELECT * FROM alunos WHERE idade < 30
Nome Idade 1 Rodrigo 30 2 Patricia 20 3 Marcos 33 4 Ricardo 28 SELECT * FROM alunos WHERE idade < 30 declarativa
menores = [] ! for (int i = 1; i <= 4; i++) { aluno = alunos[i]; if (aluno.idade < 30) { menores.add(aluno); } } ! return menores; alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ] SELECT * FROM alunos WHERE idade < 30
var numbers = [1,2,3,4,5]
var numbers = [1,2,3,4,5] var doubled = [] ! for(var i = 0; i < numbers.length; i++) { var newNumber = numbers[i] * 2 doubled.push(newNumber) } console.log(doubled) //=> [2,4,6,8,10]
var numbers = [1,2,3,4,5] var doubled = [] ! for(var i = 0; i < numbers.length; i++) { var newNumber = numbers[i] * 2 doubled.push(newNumber) } console.log(doubled) //=> [2,4,6,8,10] var numbers = [1,2,3,4,5] var doubled = numbers.map(function(n) { return n * 2 }) console.log(doubled) //=> [2,4,6,8,10]
var numbers = [1,2,3,4,5]! ! ! ! ! !
var numbers = [1,2,3,4,5]! var total = 0! ! for(var i = 0; i < numbers.length; i++) {! total += numbers[i]! }! console.log(total) //=> 15
var numbers = [1,2,3,4,5]! var total = 0! ! for(var i = 0; i < numbers.length; i++) {! total += numbers[i]! }! console.log(total) //=> 15 var numbers = [1,2,3,4,5]! ! var total = numbers.reduce(function(sum,n){! return sum + n! });! console.log(total) //=> 15
programação imperativa 1. HOW programação declarativa WHAT
SELECT * FROM alunos WHERE idade < 30
SELECT * FROM alunos WHERE idade < 30 var doubled = [1,2,3].map(function(n) { return n * 2 })
SELECT * FROM alunos WHERE idade < 30 var doubled = [1,2,3].map(function(n) { return n * 2 }) var total = [1,2,3].reduce(function(sum,n){! return sum + n! });
function f (x) { return function (y) { return x + y; }; } var a = f(5); var b = a(3); alert(b); // ????
first-class functions
higher-order function
:closures:
a closure (...) is a function or reference to a function together with a referencing environment “ http://en.wikipedia.org/wiki/Closure_(computer_science)
:closure:
Por quê?!
#comofas?!
a closure (...) is a function or reference to a function together with a referencing environment “
closure = function + environment
closure = function + environment
(
= = =
)
closure = function + environment
...
E o Kiko ?!
encapsulamento, que é bom....
>> “Alexandre Gomes” >> undefined >> undefined
Yahoo Javascript Module Pattern
P&Raprender.unb.br
Programação Funcional com Javascript

Programação Funcional com Javascript

  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
    Object ! Prototipo de Object ! pO= 1 Object.prototype.pO = 1
  • 7.
    A ! a = 2 varA = function() { this.a = 2; } Object ! Prototipo de Object ! pO = 1
  • 8.
    A ! a = 2 Prototipode A ! Object ! Prototipo de Object ! pO = 1
  • 9.
    A ! a = 2 Prototipode A ! A.prototype.pA = 3 Object ! Prototipo de Object ! pO = 1
  • 10.
    A ! a = 2 Prototipode A ! pA = 3 A.prototype.pA = 3 Object ! Prototipo de Object ! pO = 1
  • 11.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 var B = function() { this.b = 4; } B ! b = 4
  • 12.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 Prototipo de B !
  • 13.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 Prototipo de B ! B.prototype = new A
  • 14.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A
  • 15.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A
  • 16.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A new A() ! a = 2
  • 17.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 B.prototype = new A new A() ! a = 2
  • 18.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 B.prototype.pB = 5
  • 19.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 B.prototype.pB = 5
  • 20.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5
  • 21.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x = new B()
  • 22.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x = new B() x new B() ! b = 4
  • 23.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x = new B() x new B() ! b = 4 Prototipo de B !
  • 24.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 25.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.b x new B() ! b = 4 Prototipo de B ! ?
  • 26.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 27.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.pB x new B() ! b = 4 Prototipo de B ! ?
  • 28.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 29.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.a x new B() ! b = 4 Prototipo de B ! ?
  • 30.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 31.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.pA x new B() ! b = 4 Prototipo de B ! ?
  • 32.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x new B() ! b = 4 Prototipo de B !
  • 33.
    A ! a = 2 Prototipode A ! pA = 3 Object ! Prototipo de Object ! pO = 1 B ! b = 4 new A() ! a = 2 pB = 5 x.pO x new B() ! b = 4 Prototipo de B ! ?
  • 35.
    function f (x){ return function (y) { return x + y; }; } var a = f(5); var b = a(3); alert(b); // ????
  • 36.
    Javascript Alexandre Gomes princípios daprogramação funcional com
  • 37.
    programação imperativa 1. Batabem todos os ingredientes ( menos o fermento) da massa no liquidificador, aproximadamente 2 a 3 minutos 2. Acrescente o fermento e bata por mais uns 15 segundos 3. Coloque em uma fôrma redonda, u n t a d a c o m m a n t e i g a e polvilhada com farinha de trigo 4. Asse por cerca de 40 minutos em forno médio (180º graus), preaquecido programação declarativa Comunique assim que estiver pronto. Não deixe o bolo queimar. Não deixe que abram o forno antes da hora.
  • 38.
    Buscar alunos commenos de 30 anos. alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ]
  • 39.
    menores = [] ! for(int i = 1; i <= 4; i++) { aluno = alunos[i]; if (aluno.idade < 30) { menores.add(aluno); } } ! return menores; alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ]
  • 40.
    menores = [] ! for(int i = 1; i <= 4; i++) { aluno = alunos[i]; if (aluno.idade < 30) { menores.add(aluno); } } ! return menores; alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ] imperativa
  • 41.
    alunos = [{nome:“Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ] Nome Idade 1 Rodrigo 30 2 Patricia 20 3 Marcos 33 4 Ricardo 28
  • 42.
    Nome Idade 1 Rodrigo30 2 Patricia 20 3 Marcos 33 4 Ricardo 28 SELECT * FROM alunos WHERE idade < 30
  • 43.
    Nome Idade 1 Rodrigo30 2 Patricia 20 3 Marcos 33 4 Ricardo 28 SELECT * FROM alunos WHERE idade < 30 declarativa
  • 44.
    menores = [] ! for(int i = 1; i <= 4; i++) { aluno = alunos[i]; if (aluno.idade < 30) { menores.add(aluno); } } ! return menores; alunos = [{nome: “Rodrigo”, idade: 30}, {nome: “Patricia”, idade: 20}, {nome: “Marcos”, idade: 33}, {nome: “Ricardo”, idade: 28} ] SELECT * FROM alunos WHERE idade < 30
  • 46.
    var numbers =[1,2,3,4,5]
  • 47.
    var numbers =[1,2,3,4,5] var doubled = [] ! for(var i = 0; i < numbers.length; i++) { var newNumber = numbers[i] * 2 doubled.push(newNumber) } console.log(doubled) //=> [2,4,6,8,10]
  • 48.
    var numbers =[1,2,3,4,5] var doubled = [] ! for(var i = 0; i < numbers.length; i++) { var newNumber = numbers[i] * 2 doubled.push(newNumber) } console.log(doubled) //=> [2,4,6,8,10] var numbers = [1,2,3,4,5] var doubled = numbers.map(function(n) { return n * 2 }) console.log(doubled) //=> [2,4,6,8,10]
  • 49.
    var numbers =[1,2,3,4,5]! ! ! ! ! !
  • 50.
    var numbers =[1,2,3,4,5]! var total = 0! ! for(var i = 0; i < numbers.length; i++) {! total += numbers[i]! }! console.log(total) //=> 15
  • 51.
    var numbers =[1,2,3,4,5]! var total = 0! ! for(var i = 0; i < numbers.length; i++) {! total += numbers[i]! }! console.log(total) //=> 15 var numbers = [1,2,3,4,5]! ! var total = numbers.reduce(function(sum,n){! return sum + n! });! console.log(total) //=> 15
  • 52.
  • 53.
    SELECT * FROMalunos WHERE idade < 30
  • 54.
    SELECT * FROMalunos WHERE idade < 30 var doubled = [1,2,3].map(function(n) { return n * 2 })
  • 55.
    SELECT * FROMalunos WHERE idade < 30 var doubled = [1,2,3].map(function(n) { return n * 2 }) var total = [1,2,3].reduce(function(sum,n){! return sum + n! });
  • 57.
    function f (x){ return function (y) { return x + y; }; } var a = f(5); var b = a(3); alert(b); // ????
  • 58.
  • 60.
  • 68.
  • 76.
    a closure (...)is a function or reference to a function together with a referencing environment “ http://en.wikipedia.org/wiki/Closure_(computer_science)
  • 80.
  • 85.
  • 89.
  • 92.
    a closure (...)is a function or reference to a function together with a referencing environment “
  • 93.
    closure = function+ environment
  • 94.
    closure = function+ environment
  • 97.
  • 107.
  • 108.
  • 110.
    closure = function+ environment
  • 114.
  • 115.
  • 122.
  • 124.
    >> “Alexandre Gomes” >>undefined >> undefined
  • 126.
  • 127.