Weitere ähnliche Inhalte Ähnlich wie Programação Funcional (20) Kürzlich hochgeladen (20) Programação Funcional9. Ler um arquivo, listar
as palavras mais
comuns em ordem
decrescente.
Sunday, July 14, 13
10. Ler um arquivo, listar
as palavras mais
comuns em ordem
decrescente.
Sunday, July 14, 13
14. Donald Knuth Doug McIlroy
X
10+ páginas de Pascal/WEB 6 linhas de shell
Sunday, July 14, 13
15. tr -cs A-Za-z 'n' |
tr A-Z a-z |
sort |
uniq -c |
sort -rn |
sed ${1}q
Sunday, July 14, 13
17. $ 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
18. “Keep it simple, make
it general, and make it
intelligible.”
Doug McIlroy
Sunday, July 14, 13
19. “Keep it simple, make
it general, and make it
intelligible.”
Doug McIlroy
Sunday, July 14, 13
24. 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
30. 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
38. Higher Order Function
pessoas = [{'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
41. Tail Call Optimization
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
42. 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
44. Currying & Partials
def to_tag(tag, texto):
return "<{tag}>{texto}</{tag}>".format(tag=tag,
texto=texto)
Sunday, July 14, 13
45. 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
46. 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
47. 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
48. 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
55. Laziness
def from(n: Int): Stream[Int] = n #:: from(n+1)
val nats = from(0)
def sieve(s: Stream[Int]): Stream[Int] =
Sunday, July 14, 13
56. Laziness
def 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
57. Laziness
def 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
58. Laziness
def 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
59. Laziness
def 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
60. Laziness
def 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
62. Data Abstraction
class 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
63. Data Abstraction
class 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()
>>> um
0 + 1
>>> dois
0 + 1 + 1
>>> um + dois
0 + 1 + 1 + 1
Sunday, July 14, 13
71. 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