Programação Funcional com Javascript

Preview:

Citation preview

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.bx

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.pBx

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.ax

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 Idade1 Rodrigo 302 Patricia 203 Marcos 334 Ricardo 28

Nome Idade1 Rodrigo 302 Patricia 203 Marcos 334 Ricardo 28

SELECT * FROM alunos WHERE idade < 30

Nome Idade1 Rodrigo 302 Patricia 203 Marcos 334 Ricardo 28

SELECT * FROM alunos WHERE idade < 30

declarativ

a

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-classfunctions

higher-orderfunction

: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