Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 ·...
Transcript of Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 ·...
![Page 1: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/1.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Python funcional
Jesus Espino Garcıa
8 de Noviembre de 2014
Jesus Espino Garcıa Python funcional
![Page 2: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/2.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Introduccion
Introduccion
Jesus Espino Garcıa Python funcional
![Page 3: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/3.jpg)
IntroduccionPython funcional
fn.pyPara terminar
¿Que significa funcional?
Programacion con funciones (matematicas)
Funciones puras (mismas entradas, mismas salidas).
Inmutabilidad.
Ausencia de estado.
Jesus Espino Garcıa Python funcional
![Page 4: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/4.jpg)
IntroduccionPython funcional
fn.pyPara terminar
¿Por que?
Paralelizacion.
Facilitar el testing.
Reusabilidad.
Depuracion mas facil.
Estado muy controlado.
Jesus Espino Garcıa Python funcional
![Page 5: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/5.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Tıpicas estrategias funcionales
Combinacion y composicion de funciones pequenas.
Datos + funciones transformadoras.
Aplicacion de transformaciones mediante orden superior.
Uso de funciones inline.
Acotado de efectos laterales.
Tendencia al uso de funciones puras.
Jesus Espino Garcıa Python funcional
![Page 6: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/6.jpg)
IntroduccionPython funcional
fn.pyPara terminar
¿Que necesito?
Funciones como ciudadanos de primera (son un objeto mas).
Jesus Espino Garcıa Python funcional
![Page 7: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/7.jpg)
IntroduccionPython funcional
fn.pyPara terminar
¿Es python un lenguaje funcional?
No.
Es un lenguaje multi-paradigma.
Soporta algunas caracterısticas funcionales.
Permite hacer programacion funcional.
Carece de caracterısticas avanzadas presentes en lenguajes funcionales.
Jesus Espino Garcıa Python funcional
![Page 8: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/8.jpg)
IntroduccionPython funcional
fn.pyPara terminar
¿Que me dan los lenguajes funcionales?
Estructuras inmutables eficientes.
Funciones de orden superior.
Pattern matching.
TCO: Tail call optimization.
Aplicacion parcial y currificacion.
Control de efectos laterales.
Funciones lambda.
Evaluacion perezosa.
Composicion de funciones.
Jesus Espino Garcıa Python funcional
![Page 9: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/9.jpg)
IntroduccionPython funcional
fn.pyPara terminar
¿Que me da python?
Evaluacion perezosa (Limitada).
Aplicacion parcial.
Funciones lambda.
Funciones de orden superior.
Jesus Espino Garcıa Python funcional
![Page 10: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/10.jpg)
IntroduccionPython funcional
fn.pyPara terminar
¿Que me da fn.py?
Estructuras inmutables eficientes (En desarrollo).
TCO: Tail call optimization.
Aplicacion parcial y currificacion.
Composicion de funciones.
Funciones lambda (Al estilo de Scala).
Jesus Espino Garcıa Python funcional
![Page 11: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/11.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Funcional vs. Imperativo
Imperativo
x = sum(1, 2)
y = sum(x, 3)
z = prod(y, 4)
print(z)
Funcionalprint(prod(sum(sum(1,2),3), 4))
Jesus Espino Garcıa Python funcional
![Page 12: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/12.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Funcional vs. Imperativo
Funcional con composicion y aplicacion parcial
func = F(sum, 1, 2) >> F(sum, 3) >> F(prod, 4) >> print
func()
Funcional con currificacionprod4 = prod(4)
sum3 = sum(3)
sum2 = sum(2)
print(prod4(sum3(sum2(1))))
Jesus Espino Garcıa Python funcional
![Page 13: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/13.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Python funcional
Python funcional
Jesus Espino Garcıa Python funcional
![Page 14: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/14.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Evaluacion perezosa
Iteradores
Generadores
Jesus Espino Garcıa Python funcional
![Page 15: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/15.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Evaluacion perezosa
Iteradores>>> i = map(print, [1,2,3])
>>> next(i)
1
>>> i = map(print, [1,2,3])
>>> list(i)
1
2
3
[None, None, None]
Jesus Espino Garcıa Python funcional
![Page 16: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/16.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Evaluacion perezosa
Generadores>>> import itertools
>>> def generate():
... x = 0
... while True:
... yield x
... x += 1
>>> numbers = generate()
>>> list(itertools.takewhile(lambda x: x < 10, numbers))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(itertools.takewhile(lambda x: x < 12, numbers))
[11]
Jesus Espino Garcıa Python funcional
![Page 17: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/17.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Aplicacion parcial
Aplicacion parcial>>> from functools import partial
>>> from opertor import add
>>> add4 = partial(add, 4)
>>> add4(3)
7
>>> print_noline = partial(print, end="")
>>> print_noline("hello")
hello>>>
Jesus Espino Garcıa Python funcional
![Page 18: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/18.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Funciones lambda
Funciones lambda>>> pow2 = lambda x: x**2
>>> pow2(10)
100
Jesus Espino Garcıa Python funcional
![Page 19: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/19.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Funciones de orden superior
map
filter
sorted
reduce
decorators
Jesus Espino Garcıa Python funcional
![Page 20: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/20.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Funciones de orden superior
Funciones de orden superior
>>> list(map(lambda x: x**2, [1, 2, 3]))
[1, 4, 9]
>>> list(filter(lambda x: x > 1, [1, 2, 3]))
[2, 3]
>>> sorted([2, 1, 3], key=lambda x: x)
[1, 2, 3]
>>> sorted([1, 2, 3], key=lambda x: -x)
[3, 2, 1]
>>> from functools import reduce
>>> reduce(lambda x, y: x + y, [1, 2, 3])
6
>>> from functools import lru_cache
>>> cached_sum = lru_cache()(lambda x: sum(range(x)))
>>> cached_sum(4)
6
Jesus Espino Garcıa Python funcional
![Page 21: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/21.jpg)
IntroduccionPython funcional
fn.pyPara terminar
fn.py
fn.py
Jesus Espino Garcıa Python funcional
![Page 22: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/22.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Estructuras inmutables
LinkedList
Stack
Queue
Deque
Vector
SkewHeap
PairingHeap
Jesus Espino Garcıa Python funcional
![Page 23: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/23.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Estructuras inmutables
LinkedList>>> from fn.immutable import LinkedList
>>> l = LinkedList()
>>> l.head
>>> l.tail
>>> l2 = l.cons(10)
>>> l2.head
10
>>> l2.tail
<fn.immutable.list.LinkedList object at 0x7f3927e59f08>
>>> l.head
>>> l.tail
Jesus Espino Garcıa Python funcional
![Page 24: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/24.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Estructuras inmutables
Stack>>> from fn.immutable import Stack
>>> s = Stack()
>>> s.head
>>> s.tail
>>> s2 = s.push(10)
>>> s2.head
10
>>> s2.tail
<fn.immutable.list.Stack object at 0x7f3926ae9818>
>>> (value, s3) = s2.pop()
>>> value
10
Jesus Espino Garcıa Python funcional
![Page 25: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/25.jpg)
IntroduccionPython funcional
fn.pyPara terminar
TCO
Recursion normaldef fact(n):
if n == 0: return 1
return n * fact(n-1)
TCOfrom fn import recur
@recur.tco
def fact(n, acc=1):
if n == 0: return False, acc
return True, (n-1, acc*n)
Jesus Espino Garcıa Python funcional
![Page 26: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/26.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Aplicacion parcial
Aplicacion parcial>>> from fn import F
>>> from operator import add
>>> add2 = F(add, 2)
>>> add2(3)
5
Jesus Espino Garcıa Python funcional
![Page 27: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/27.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Currificacion
Currificacion>>> from fn.func import curried
>>> curry_add = curried(lambda x, y: x + y)
>>> curry_add(2)(3)
5
>>> @curried
... def curried_add(x, y):
... return x + y
...
>>> curried_add(2)(3)
5
Jesus Espino Garcıa Python funcional
![Page 28: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/28.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Composicion de funciones
Composicion normal
>>> myfunc = lambda x: duplicate(add2(x))
>>> myfunc(3)
10
Composicion al estilo fn.py
>>> myfunc = F(duplicate) << add2
>>> myfunc(3)
10
>>> myfunc = F(add2) >> duplicate
>>> myfunc(3)
10
Jesus Espino Garcıa Python funcional
![Page 29: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/29.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Funciones lambda al estilo scala
Funciones lambda al estilo scala>>> from fn import _
>>> (_ + _)(2, 3)
5
>>> list(map(_ + 2, [1, 2, 3]))
[3, 4, 5]
Jesus Espino Garcıa Python funcional
![Page 30: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/30.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Para terminar
Para terminar
Jesus Espino Garcıa Python funcional
![Page 31: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/31.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Conclusiones
Python permite programar de forma funcional.
Fn.py nos da las herramientas para llegar un poco mas lejos.
Python + Fn.py se queda lejos de lenguajes como Erlang, Clojure oHaskell.
Lo que se puede aplicar en Python es una mejora significativa sobre elcodigo.
Jesus Espino Garcıa Python funcional
![Page 32: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/32.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Referencias
https://github.com/kachayev/fn.py: Fn.py
https://docs.python.org/3/howto/functional.html: Howto deprogramacion funcional.
http://kachayev.github.io/talks/uapycon2012/: Charla de AlexeyKachayev
Jesus Espino Garcıa Python funcional
![Page 33: Jesus Espino Garc a2014.es.pycon.org/static/talks/python-funcional - Jesús... · 2015-05-20 · Carece de caracter sticas avanzadas presentes en lenguajes funcionales. Jesus Espino](https://reader033.fdocumento.com/reader033/viewer/2022041617/5e3c9bf296e2916724420ad7/html5/thumbnails/33.jpg)
IntroduccionPython funcional
fn.pyPara terminar
Dudas
. . .
Jesus Espino Garcıa Python funcional