Programação Funcional

71
Programação Funcional FISL 2013 Juarez Bochi λ Sunday, July 14, 13

description

Programação Funcional FISL 2013

Transcript of Programação Funcional

Page 1: Programação Funcional

Programação FuncionalFISL 2013

Juarez Bochi

λSunday, July 14, 13

Page 2: Programação Funcional

Juarez Bochi

Sunday, July 14, 13

Page 3: Programação Funcional

Juarez Bochi

Sunday, July 14, 13

Page 4: Programação Funcional

Juarez Bochi

Sunday, July 14, 13

Page 5: Programação Funcional

Juarez Bochi

Sunday, July 14, 13

Page 6: Programação Funcional

Juarez Bochi

Sunday, July 14, 13

Page 7: Programação Funcional

Agenda

• Motivação

• Conceitos

• Exemplos

• Resumo

Sunday, July 14, 13

Page 8: Programação Funcional

Motivação

Sunday, July 14, 13

Page 9: Programação Funcional

Ler um arquivo, listar as palavras mais

comuns em ordem decrescente.

Sunday, July 14, 13

Page 10: Programação Funcional

Ler um arquivo, listar as palavras mais

comuns em ordem decrescente.

Sunday, July 14, 13

Page 11: Programação Funcional

Donald Knuth

X

Sunday, July 14, 13

Page 12: Programação Funcional

Donald Knuth Doug McIlroy

X

Sunday, July 14, 13

Page 13: Programação Funcional

Donald Knuth Doug McIlroy

X

10+ páginas de Pascal/WEBSunday, July 14, 13

Page 14: Programação Funcional

Donald Knuth Doug McIlroy

X

10+ páginas de Pascal/WEB 6 linhas de shellSunday, July 14, 13

Page 15: Programação Funcional

tr -cs A-Za-z '\n' |tr A-Z a-z |sort |uniq -c |sort -rn |sed ${1}q

Sunday, July 14, 13

Page 16: Programação Funcional

Sunday, July 14, 13

Page 17: Programação Funcional

$ cat discurso.txt | tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed 40q 65 e 48 de 42 a 35 o 32 que 18 os 15 para 15 com 14 mais 13 nao 13 do 11 as 10 um 10 dos 10 da 9 das 8 ela 7 todos 7 se 7 pais 7 muito 6 ruas 6 brasil 5 violencia

Sunday, July 14, 13

Page 18: Programação Funcional

“Keep it simple, make it general, and make it intelligible.” Doug McIlroy

Sunday, July 14, 13

Page 19: Programação Funcional

“Keep it simple, make it general, and make it intelligible.” Doug McIlroy

Sunday, July 14, 13

Page 20: Programação Funcional

Conceitos

Sunday, July 14, 13

Page 21: Programação Funcional

Paradigmas

• Imperativo

• Lógico

• Funcional

• Orientado a Objetos

Sunday, July 14, 13

Page 22: Programação Funcional

Paradigma Imperativo

Linguagem Computador

Variáveis Mutáveis Endereço de memória

Estruturas de controle (if-then-else, loop)

Jumps

Sunday, July 14, 13

Page 23: Programação Funcional

“Can Programming Be Liberated from the von. Neumann Style?”John Backus - Turing Award Lecture

Sunday, July 14, 13

Page 24: Programação Funcional

Programação Funcional

• Concentrar em teorias, não mutações

• Minimizar mudança de estados

• Sem estruturas de controle imperativas

• Foco em funções

Sunday, July 14, 13

Page 25: Programação Funcional

“Programação Funcional é ortogonal à Orientação

a Objetos”

Sunday, July 14, 13

Page 26: Programação Funcional

Elementos de Programação

• Expressões Primitivas

• Meios de Combinação

• Meios de Abstração

Sunday, July 14, 13

Page 27: Programação Funcional

Exemplos

Sunday, July 14, 13

Page 28: Programação Funcional

First Class Functions

Sunday, July 14, 13

Page 29: Programação Funcional

First Class Functions

Sunday, July 14, 13

Page 30: Programação Funcional

Closure> var inc, dec;undefined> function contador() {... var x = 0;... inc = function() { return ++x; };... dec = function() { return --x; };... }undefined> contador();undefined

Sunday, July 14, 13

Page 31: Programação Funcional

Closure> inc();1> inc();2> dec();1> dec();0> inc();1> xReferenceError: x is not defined

Sunday, July 14, 13

Page 32: Programação Funcional

> import Control.Applicative> let foo = fmap (+3) (+2)> foo 1015

Sunday, July 14, 13

Page 33: Programação Funcional

> import Control.Applicative> let foo = fmap (+3) (+2)> foo 1015

Sunday, July 14, 13

Page 34: Programação Funcional

> (defn soma3 [x] (+ x 3))

Sunday, July 14, 13

Page 35: Programação Funcional

> (defn soma3 [x] (+ x 3))

> (map soma3 [2 4 6])(5 7 9)

Sunday, July 14, 13

Page 36: Programação Funcional

> (defn soma3 [x] (+ x 3))

> (map soma3 [2 4 6])(5 7 9)

Sunday, July 14, 13

Page 37: Programação Funcional

> (defn soma3 [x] (+ x 3))

> (map soma3 [2 4 6])(5 7 9)

> (pmap soma3 [2 4 6])(5 7 9)

Sunday, July 14, 13

Page 38: Programação Funcional

Higher Order Functionpessoas = [{'nome': 'Adolfo', 'estado': 'MG'}, {'nome': 'Pedro', 'estado': 'RS'}, {'nome': 'Maria', 'estado': 'AC'}]

def por_estado(pessoa1, pessoa2): return cmp(pessoa1['estado'], pessoa2['estado'])

>>> pprint.pprint(sorted(pessoas, cmp=por_estado))[{'estado': 'AC', 'nome': 'Maria'}, {'estado': 'MG', 'nome': 'Adolfo'}, {'estado': 'RS', 'nome': 'Pedro'}]

Sunday, July 14, 13

Page 39: Programação Funcional

Recursão

Sunday, July 14, 13

Page 40: Programação Funcional

Recursão

(define (fib n) .. (if (< n 2) .. n .. (+ (fib (- n 1)) (fib (- n 2))))) (fib 10)=> 55

Sunday, July 14, 13

Page 41: Programação Funcional

Tail Call Optimization1. fib(5)2. fib(4) + fib(3)3. (fib(3) + fib(2)) + (fib(2) + fib(1))4. ((fib(2) + fib(1)) + (fib(1) + fib(0))) + ((fibb(1) + fib(0)) + fib(1))5. (((fib(1) + fib(0)) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) +

fib(1))

Sunday, July 14, 13

Page 42: Programação Funcional

Tail Call Optimization

(define (fib n) .. (letrec ((fib-aux (lambda (n a b) .. (if (= n 0) .. a .. (fib-aux (- n 1) b (+ a b)))))) .. (fib-aux n 0 1))) (fib 1000)=> 4.346655768693743e+208

1. fib(5)2. fib(4) + fib(3)3. (fib(3) + fib(2)) + (fib(2) + fib(1))4. ((fib(2) + fib(1)) + (fib(1) + fib(0))) + ((fibb(1) + fib(0)) + fib(1))5. (((fib(1) + fib(0)) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) +

fib(1))

Sunday, July 14, 13

Page 43: Programação Funcional

Sunday, July 14, 13

Page 44: Programação Funcional

Currying & Partialsdef to_tag(tag, texto): return "<{tag}>{texto}</{tag}>".format(tag=tag, texto=texto)

Sunday, July 14, 13

Page 45: Programação Funcional

Currying & Partials

def partial(funcao, argumento): def fn(arg): return funcao(argumento, arg) return fn

def to_tag(tag, texto): return "<{tag}>{texto}</{tag}>".format(tag=tag, texto=texto)

Sunday, July 14, 13

Page 46: Programação Funcional

Currying & Partials

def partial(funcao, argumento): def fn(arg): return funcao(argumento, arg) return fn

def to_tag(tag, texto): return "<{tag}>{texto}</{tag}>".format(tag=tag, texto=texto)

negrito = partial(to_tag, 'b')italico = partial(to_tag, 'i')

Sunday, July 14, 13

Page 47: Programação Funcional

Currying & Partials

def partial(funcao, argumento): def fn(arg): return funcao(argumento, arg) return fn

def to_tag(tag, texto): return "<{tag}>{texto}</{tag}>".format(tag=tag, texto=texto)

negrito = partial(to_tag, 'b')italico = partial(to_tag, 'i')>>> negrito(italico("Oi, FISL!"))"<b><i>Oi FISL!</i></b>"

Sunday, July 14, 13

Page 48: Programação Funcional

Currying & Partials

def partial(funcao, argumento): def fn(arg): return funcao(argumento, arg) return fn

def to_tag(tag, texto): return "<{tag}>{texto}</{tag}>".format(tag=tag, texto=texto)

DSL!!negrito = partial(to_tag, 'b')italico = partial(to_tag, 'i')>>> negrito(italico("Oi, FISL!"))"<b><i>Oi FISL!</i></b>"

Sunday, July 14, 13

Page 49: Programação Funcional

Laziness

Sunday, July 14, 13

Page 50: Programação Funcional

Laziness

Sunday, July 14, 13

Page 51: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1)

Sunday, July 14, 13

Page 52: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1) 

Sunday, July 14, 13

Page 53: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1) val nats = from(0)

Sunday, July 14, 13

Page 54: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1) val nats = from(0) 

Sunday, July 14, 13

Page 55: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1) val nats = from(0) def sieve(s: Stream[Int]): Stream[Int] =

Sunday, July 14, 13

Page 56: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1) val nats = from(0) def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.tail filter (_ % s.head != 0))

Sunday, July 14, 13

Page 57: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1) val nats = from(0) def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.tail filter (_ % s.head != 0)) 

Sunday, July 14, 13

Page 58: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1) val nats = from(0) def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.tail filter (_ % s.head != 0)) val primes = sieve(from(2))

Sunday, July 14, 13

Page 59: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1) val nats = from(0) def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.tail filter (_ % s.head != 0)) val primes = sieve(from(2)) 

Sunday, July 14, 13

Page 60: Programação Funcional

Lazinessdef from(n: Int): Stream[Int] = n #:: from(n+1) val nats = from(0) def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.tail filter (_ % s.head != 0)) val primes = sieve(from(2)) (primes take N).toList

Sunday, July 14, 13

Page 61: Programação Funcional

Data Abstractionclass Zero(Natural): def __init__(self): pass

def __repr__(self): return "0"

def __add__(self, other): return other

Sunday, July 14, 13

Page 62: Programação Funcional

Data Abstractionclass Zero(Natural): def __init__(self): pass

def __repr__(self): return "0"

def __add__(self, other): return other

class Natural(object): def __init__(self, anterior): self.anterior = anterior

def __repr__(self): return repr(self.anterior) + " + 1"

def __add__(self, other): return self.anterior + other.sucessor()

def sucessor(self): return Natural(anterior=self)

Sunday, July 14, 13

Page 63: Programação Funcional

Data Abstractionclass Zero(Natural): def __init__(self): pass

def __repr__(self): return "0"

def __add__(self, other): return other

class Natural(object): def __init__(self, anterior): self.anterior = anterior

def __repr__(self): return repr(self.anterior) + " + 1"

def __add__(self, other): return self.anterior + other.sucessor()

def sucessor(self): return Natural(anterior=self)>>> zero = Zero()

>>> um = zero.sucessor()>>> dois = um.sucessor()>>> um0 + 1>>> dois0 + 1 + 1>>> um + dois0 + 1 + 1 + 1

Sunday, July 14, 13

Page 64: Programação Funcional

Resumo

Sunday, July 14, 13

Page 65: Programação Funcional

Blub Paradox

Sunday, July 14, 13

Page 66: Programação Funcional

Resumo

Código fácil de entender

Sunday, July 14, 13

Page 67: Programação Funcional

Resumo

Código fácil de manter

Sunday, July 14, 13

Page 68: Programação Funcional

Resumo

Código fácil de testar

Sunday, July 14, 13

Page 69: Programação Funcional

Resumo

Código fácil de escalar

Sunday, July 14, 13

Page 70: Programação Funcional

Obrigado!@jbochi

Sunday, July 14, 13

Page 71: Programação Funcional

Referências• http://www.leancrew.com/all-this/2011/12/more-shell-less-egg/

• http://onesixtythree.com/literate/literate2.pdf

• http://mitpress.mit.edu/sicp/

• http://www.paulgraham.com/avg.html

• https://www.coursera.org/course/progfun

• http://www.slideshare.net/jbochi/programao-funcional-em-python

• https://raw.github.com/richhickey/slides/master/simplicitymatters.pdf

• http://pragprog.com/magazines/2013-01/functional-programming-basics

• http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

• http://en.literateprograms.org/Fibonacci_numbers_(Scheme)

• http://norvig.com/21-days.html

• http://www.youtube.com/watch?v=3jg1AheF4n0

• http://www.flickr.com/photos/niceric/74977685/sizes/l/in/

Sunday, July 14, 13