functional programming with python (rupy 2008)
DESCRIPTION
Presented at RuPy 2008 conference in Poznań.TRANSCRIPT
![Page 1: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/1.jpg)
Functional Programming with Python
Functional Programming with PythonWhy It’s Good To Be Lazy?
Adam [email protected]
RuPy, Poznan, April 13th 2008
![Page 2: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/2.jpg)
Functional Programming with Python
Introduction
Programming paradigms
Algorithm
How to explain your granny what is programming?
Algorithm is a recipe how to cook a program
Actually computer works this way (machine language)
Called imperative programming
![Page 3: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/3.jpg)
Functional Programming with Python
Introduction
Programming paradigms
Functional programming
Functional programming is a more abstract approach
Program seen as evaluations of mathematical functions
More focused on what to compute than how to compute
![Page 4: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/4.jpg)
Functional Programming with Python
Introduction
Functional programming
Features of functional languages
Functions as first-class objects
Support for high-order functions
Recursion used instead of loop constructs
Lists as basic data structures (see Lisp)
Avoiding side effects (global state)
![Page 5: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/5.jpg)
Functional Programming with Python
Introduction
Functional programming
Benefits of stateless programs
Idempotent functions
Order of evaluation not defined
Lazy evaluation possible
Optimizations
Concurrent processing
Easier to test and debug
Side effects can’t be eliminated, but can be isolated (monads)
![Page 6: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/6.jpg)
Functional Programming with Python
Introduction
Functional programming
Theoretical models
Turing machine (Alan Turing)
Lambda λ calculus (Alonzo Church)
TRUE := λxy .xFALSE := λxy .y
Computationally equivalent (Church-Turing thesis)
![Page 7: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/7.jpg)
Functional Programming with Python
Introduction
Functional programming
What about the real world?
Functional programming is not mainstream
Pure functional programming is difficult
Languages borrow concepts from the functional world
Recent revival due to a need for concurrency (Erlang)
It widens your perspective on programming
Python has some functional features too...
![Page 8: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/8.jpg)
Functional Programming with Python
Introduction
Functional programming
What about the real world?
Functional programming is not mainstream
Pure functional programming is difficult
Languages borrow concepts from the functional world
Recent revival due to a need for concurrency (Erlang)
It widens your perspective on programming
Python has some functional features too...
![Page 9: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/9.jpg)
Functional Programming with Python
Functional Python
Functions as objects
First-class functions
Lambda defines an anonymous function
def square(x):
return x**2
equivalent to
square = lambda x: x**2
![Page 10: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/10.jpg)
Functional Programming with Python
Functional Python
Functions as objects
First-class functions
Lambda defines an anonymous function
def square(x):
return x**2
equivalent to
square = lambda x: x**2
![Page 11: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/11.jpg)
Functional Programming with Python
Functional Python
Functions as objects
Closures
Closure is a function with environment attached
def build_taxer(rate):
def taxer(amount):
return amount * (float(rate) / 100)
return taxer
tax1 = build_taxer(22)
tax2 = build_taxer(7)
![Page 12: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/12.jpg)
Functional Programming with Python
Functional Python
Functions as objects
Closures
Closure is a function with environment attached
def build_taxer(rate):
def taxer(amount):
return amount * (float(rate) / 100)
return taxer
tax1 = build_taxer(22)
tax2 = build_taxer(7)
![Page 13: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/13.jpg)
Functional Programming with Python
Functional Python
Prime numbers
Prime numbers
Definition
Natural number n is prime iff
¬∃k ∈ [2, n) : n ≡ 0 mod k
How to translate this into code?
![Page 14: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/14.jpg)
Functional Programming with Python
Functional Python
Prime numbers
Imperative primes
def is_prime(n):
k = 2
while k < n:
if n % k == 0:
return False
k += 1
return True
List of statements to execute one after another
Not obvious if and when the loop ends
Local side effects
![Page 15: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/15.jpg)
Functional Programming with Python
Functional Python
Prime numbers
Imperative primes
def is_prime(n):
k = 2
while k < n:
if n % k == 0:
return False
k += 1
return True
List of statements to execute one after another
Not obvious if and when the loop ends
Local side effects
![Page 16: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/16.jpg)
Functional Programming with Python
Functional Python
Map, filter and reduce
Map, filter and reduce
Higher-order functions operating on lists (sequences)
Apply a function to every elementmap(lambda x: x**2, range(1,5))
-> [1, 4, 9, 16]
Select elements matching the predicatefilter(lambda x: x%2==0, range(10))
-> [0, 2, 4, 6, 8]
Cumulatively reduce elements to a single valuereduce(lambda x,y: x+y, [7, 3, 12])
-> 22
![Page 17: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/17.jpg)
Functional Programming with Python
Functional Python
Map, filter and reduce
Map, filter and reduce
Higher-order functions operating on lists (sequences)
Apply a function to every elementmap(lambda x: x**2, range(1,5))
-> [1, 4, 9, 16]
Select elements matching the predicatefilter(lambda x: x%2==0, range(10))
-> [0, 2, 4, 6, 8]
Cumulatively reduce elements to a single valuereduce(lambda x,y: x+y, [7, 3, 12])
-> 22
![Page 18: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/18.jpg)
Functional Programming with Python
Functional Python
Map, filter and reduce
Map, filter and reduce
Higher-order functions operating on lists (sequences)
Apply a function to every elementmap(lambda x: x**2, range(1,5))
-> [1, 4, 9, 16]
Select elements matching the predicatefilter(lambda x: x%2==0, range(10))
-> [0, 2, 4, 6, 8]
Cumulatively reduce elements to a single valuereduce(lambda x,y: x+y, [7, 3, 12])
-> 22
![Page 19: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/19.jpg)
Functional Programming with Python
Functional Python
Map, filter and reduce
Why map and reduce are so useful?
Can simplify complex loops
Can be easily chained
Many computations can be reduced to those
Can be distributed (see Google’s MapReduce)
![Page 20: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/20.jpg)
Functional Programming with Python
Functional Python
Map, filter and reduce
Primes, second approach
def is_prime(n):
len(filter(lambda k: n%k==0, range(2,n))) == 0
def primes(m):
filter(is_prime, range(1,m))
Intention of the code can be seen:“Is the list of non-trivial divisors empty?”
No side effects
return omitted for readability
![Page 21: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/21.jpg)
Functional Programming with Python
Functional Python
Map, filter and reduce
Primes, second approach
def is_prime(n):
len(filter(lambda k: n%k==0, range(2,n))) == 0
def primes(m):
filter(is_prime, range(1,m))
Intention of the code can be seen:“Is the list of non-trivial divisors empty?”
No side effects
return omitted for readability
![Page 22: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/22.jpg)
Functional Programming with Python
Functional Python
Map, filter and reduce
Primes, second approach
def is_prime(n):
len(filter(lambda k: n%k==0, range(2,n))) == 0
def primes(m):
filter(is_prime, range(1,m))
Intention of the code can be seen:“Is the list of non-trivial divisors empty?”
No side effects
return omitted for readability
![Page 23: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/23.jpg)
Functional Programming with Python
Functional Python
Map, filter and reduce
Primes, second approach
def is_prime(n):
len(filter(lambda k: n%k==0, range(2,n))) == 0
def primes(m):
filter(is_prime, range(1,m))
Intention of the code can be seen:“Is the list of non-trivial divisors empty?”
No side effects
return omitted for readability
![Page 24: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/24.jpg)
Functional Programming with Python
Functional Python
Map, filter and reduce
Primes, second approach
def is_prime(n):
len(filter(lambda k: n%k==0, range(2,n))) == 0
def primes(m):
filter(is_prime, range(1,m))
Intention of the code can be seen:“Is the list of non-trivial divisors empty?”
No side effects
return omitted for readability
![Page 25: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/25.jpg)
Functional Programming with Python
Functional Python
List comprehensions
List comprehensions
But we can do better!
List comprehensions borrowed from Haskell
[i**2 for i in range(1,10) if i%2==0]
-> [4, 16, 36, 64]
Inspired by mathematical notation (slight difference){i2| i ∈ N, i ∈ [1, 10) : i ≡ 0 mod 2}Can replace map and filter (even lambda)
Simplifies complex chains (more dimensions)
![Page 26: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/26.jpg)
Functional Programming with Python
Functional Python
List comprehensions
List comprehensions
But we can do better!
List comprehensions borrowed from Haskell
[i**2 for i in range(1,10) if i%2==0]
-> [4, 16, 36, 64]
Inspired by mathematical notation (slight difference){i2| i ∈ N, i ∈ [1, 10) : i ≡ 0 mod 2}Can replace map and filter (even lambda)
Simplifies complex chains (more dimensions)
![Page 27: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/27.jpg)
Functional Programming with Python
Functional Python
List comprehensions
List comprehensions
But we can do better!
List comprehensions borrowed from Haskell
[i**2 for i in range(1,10) if i%2==0]
-> [4, 16, 36, 64]
Inspired by mathematical notation (slight difference){i2| i ∈ N, i ∈ [1, 10) : i ≡ 0 mod 2}Can replace map and filter (even lambda)
Simplifies complex chains (more dimensions)
![Page 28: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/28.jpg)
Functional Programming with Python
Functional Python
List comprehensions
Primes, third approach
def is_prime(n):
True not in [n%k==0 for k in range(2,n)]
def primes(m):
[n for n in range(1,m) if is_prime(n)]
Is there any problem with last two versions?
Do we have to go through the whole list?
![Page 29: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/29.jpg)
Functional Programming with Python
Functional Python
List comprehensions
Primes, third approach
def is_prime(n):
True not in [n%k==0 for k in range(2,n)]
def primes(m):
[n for n in range(1,m) if is_prime(n)]
Is there any problem with last two versions?
Do we have to go through the whole list?
![Page 30: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/30.jpg)
Functional Programming with Python
Functional Python
Streams
Generators, iterators and streams
It is said that good programmers are lazy...
Iterators are lazy sequences
Generator expressions help building iterators
(i**2 for i in xrange(1,10) if i%2==0)
-> <generator object at 0x12c4850>
Map and filter will be lazy in Python 3000
Called streams in the functional world
![Page 31: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/31.jpg)
Functional Programming with Python
Functional Python
Streams
Prime numbers, fourth approach
def is_prime(n):
True not in (n%k==0 for k in xrange(2,n))
is_prime(100000000)
-> False
Lazy evaluation
![Page 32: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/32.jpg)
Functional Programming with Python
Functional Python
Streams
Prime numbers, fourth approach
def is_prime(n):
True not in (n%k==0 for k in xrange(2,n))
is_prime(100000000)
-> False
Lazy evaluation
![Page 33: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/33.jpg)
Functional Programming with Python
Functional Python
Quantification
Quantification
Can do even better with Python 2.5!
any(seq) returns true if at least one element of thesequence is true (∃, exists)
all(seq) returns true if all elements of the sequence aretrue (∀, for all)
Short-circuit evaluation, like with logical operators
![Page 34: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/34.jpg)
Functional Programming with Python
Functional Python
Quantification
Primes, grand finale
def is_prime(n):
not any(n%k==0 for k in xrange(2,n))
Does this look familiar?
Definition
Natural number n is prime iff
¬∃k ∈ [2, n) : n ≡ 0 mod k
Tadam!
![Page 35: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/35.jpg)
Functional Programming with Python
Functional Python
Quantification
Primes, grand finale
def is_prime(n):
not any(n%k==0 for k in xrange(2,n))
Does this look familiar?
Definition
Natural number n is prime iff
¬∃k ∈ [2, n) : n ≡ 0 mod k
Tadam!
![Page 36: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/36.jpg)
Functional Programming with Python
Functional Python
Quantification
Primes, grand finale
def is_prime(n):
not any(n%k==0 for k in xrange(2,n))
Does this look familiar?
Definition
Natural number n is prime iff
¬∃k ∈ [2, n) : n ≡ 0 mod k
Tadam!
![Page 37: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/37.jpg)
Functional Programming with Python
Summary
Summary
Functional programming allows you to describe theproblem in a more abstract way
Learning functional approach widens your perspective onprogramming
It’s worth applying when it makes sense
![Page 38: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/38.jpg)
Functional Programming with Python
Summary
The wizard book
http://mitpress.mit.edu/sicp/
![Page 39: Functional Programming with Python (RuPy 2008)](https://reader034.vdocuments.mx/reader034/viewer/2022042614/555028edb4c9059f318b48c0/html5/thumbnails/39.jpg)
Functional Programming with Python
Summary
Thank you
May the λ be with [email protected]://www.adambyrtek.net
http://del.icio.us/alpha.pl/functional-programming