functional python

40
The Developers Conference - POA 2015 FUNCTIONAL PYTHON Funções e conceitos aplicados 1

Upload: giovane-liberato

Post on 08-Jan-2017

734 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Functional Python

T h e D e v e l o p e r s C o n f e r e n c e - P O A 2 0 1 5

FUNCTIONAL PYTHONFunções e conceitos aplicados

1

Page 2: Functional Python

GIOVANE LIBERATO(lambda dev: dev.favorite_quote)(eu)

“git push -f origin master“

2

Page 3: Functional Python

UM CÓDIGO QUALQUER

3

Page 4: Functional Python

UM CÓDIGO QUALQUER

4

E se tivermos que adicionar mas dois, três ou cinco tipos novos no sistema?

Page 5: Functional Python

MULTIMETHOD DISPATCHER

5

Page 6: Functional Python

MULTIMETHOD DISPATCHER

6

High order functions

Page 7: Functional Python

MULTIMETHOD DISPATCHER

7

High order functions

First class functions

Page 8: Functional Python

GOTTA LOVE LIST COMPREHENSIONS

88

Page 9: Functional Python

GOTTA LOVE LIST COMPREHENSIONS

99

No side effects

Page 10: Functional Python

FUNCTION COMPOSITION

1010

Page 11: Functional Python

FUNCTION COMPOSITION

1111

f(g(x), h(x))

Page 12: Functional Python

FUNCTION COMPOSITION

1212

retirado de https://mathieularose.com/function-composition-in-python/

Page 13: Functional Python

STATELESS E IMUTABILIDADE

1313

Page 14: Functional Python

STATELESS E IMUTABILIDADE

1414

stateless arguments

imutabilidade

Page 15: Functional Python

CLOSURE

Page 16: Functional Python

DECORATORS

Page 17: Functional Python

DECORATORS

cache_values(fib(n))

Page 18: Functional Python

DECORATORS

cache_values(fib(n))

Fun fact: fib(1000) 43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875L

Page 19: Functional Python

LAZY EVALUATION

Page 20: Functional Python

SO FAR…

20

▫︎ Higher Order e First-class functions

▫︎ No side-effects

▫︎ Function composition

▫︎ Closure

▫︎ Lazy evaluation

▫︎ Recursão

Page 21: Functional Python

COMPARANDO OS CONCEITOS

FP

▫︎ Higher Order Functions

▫︎ Imutabilidade

▫︎ No side-effects

▫︎ Stateless and functions

21

OOP

▫︎ Encapsulamento

▫︎ Herança

▫︎ Polimorfismo

▫︎ State e Behaviour

Page 22: Functional Python

22

Dicas para iniciar com FP

Todas as funções devem aceitar parâmetros

Todas as funções devem retornar valores ou outra função

Loops são proibidos

Page 23: Functional Python

23

Programação funcional é útil Quando se tem um tipo fixo de dados e um número variável de operações Quando se almeja paralelismo/programação concorrente

Programação funcional não é útil Quando se tem dados e operações fixas que crescerão juntas

Page 24: Functional Python

24

Programação funcional não é difícil

Programação funcional só não é familiar

Page 25: Functional Python

Features funcionais do python

25

Page 26: Functional Python

DESCONSTRUINDO A CLASSE PYTHON

2626

retirado de http://www.pedrowerneck.com/o-porque-do-self-explicito-em-python-pt-br.html

Page 27: Functional Python

LAMBDA (FUNÇÕES ANÔNIMAS)

2727

Page 28: Functional Python

LAMBDA (FUNÇÕES ANÔNIMAS)

2828

Page 29: Functional Python

MAP

2929

Page 30: Functional Python

REDUCE

3030

Page 31: Functional Python

FILTER

3131

Page 32: Functional Python

GENERATORS

3232

Page 33: Functional Python

Mesclando OOP e FP

33

Page 34: Functional Python

STRATEGY PATTERN EM JAVA

3434

Page 35: Functional Python

Blah, muito verboso! (eu queria colocar isso no mesmo slide do código, mas não coube)

35

Page 36: Functional Python

STRATEGY PATTERN EM PYTHON

3636

Page 37: Functional Python

O QUE O PYTHON NÃO TEM

37

▫︎ Variáveis imutáveis

▫︎ Tail Recursion Optimization

▫︎ Pattern matching

Page 38: Functional Python

USANDO NO DIA A DIA

38

▫︎ No side-effects

▫︎ Funções pequenas

▫︎ Function composition

▫︎ Lazy evaluation

▫︎ Imutabilidade

Page 39: Functional Python

bit.ly/fppython

39

Page 40: Functional Python

[email protected] about.me/giovaneliberato

pixelated-project.org

OBRIGADO!