Functional Programming in Python
(PYTHON)( )
TODAY'S SLIDES
http://goo.gl/iRXD0Y
(PYTHON)( )
ABOUT ME
+ Colin Su a.k.a LittleQ
+ http://about.me/littleq
+ Productional Experience:
- Erlang
- Python
- Jav...
(PYTHON)( )
PROGRAMMING IN PYTHON
+ Imperative Programming
- Shell Scripting
+ Procedural Programming
- Languages with fun...
(PYTHON)( )
FUNCTIONAL PROGRAMMING
+ No state
+ Immutable data
+ Function as first-class citizen
+ Higher-order function
+ ...
(PYTHON)( )
IP VS FP
$ ./program1
$ ./program2 --arg=1
$ ./program3
$ ./program1 | ./program2 --arg=1 | ./program3
Imperat...
(PYTHON)( )
FUNCTION AS FIRST CLASS OBJ
def add(a, b):
return a + b
add2 = add
add2(1,2) # 3
def giveMeAdd():
def add(a, b...
(PYTHON)( )
LAMBDA SUPPORT
add = lambda a, b : a + b
minus = lambda a, b : a - b
multiply = lambda a, b : a * b
divide = l...
(PYTHON)( )
(POOR) LAMBDA SUPPORT
add = lambda a, b:
c = a + b
return c
(PYTHON)( )
(POOR) LAMBDA SUPPORT
add = lambda a, b:
c = a + b
return c
File "test.py", line 29
add = lambda a, b:
^
Synta...
(PYTHON)( )
HIGHER-ORDER FUNCTION
+ Function eats functions, and returns functions
def food():
return "food got eaten"
dri...
(PYTHON)( )
MAP/REDUCE/FILTER
+ A war between readability and conciseness
+ To make your code pluggable
(PYTHON)( )
CALCULATE "1++2+3+++4+5"
(PYTHON)( )
IMPERATIVE.PY
INPUT = "1+2++3+++4++5+6+7++8+9++10"
result = 0
for num in INPUT.split('+'):
if num:
result += i...
(PYTHON)( )
FUNCTIONAL.PY
from operator import add
INPUT = "1+2++3+++4++5+6+7++8+9++10"
print reduce(add, map(int, filter(...
(PYTHON)( )
FUNCTIONAL PYTHON
MADE SIMPLE
(PYTHON)( )
BUT NOT ALL LANGUAGES
DO...
(PYTHON)( )
IMPERATIVE.JAVA
Multiset<Integer> lengths = HashMultiset.create();
for (String string : strings) {
if (CharMat...
(PYTHON)( )
FUNCTIONAL.JAVA
Function<String, Integer> lengthFunction = new Function<String, Integer>() {
public Integer ap...
(PYTHON)( )
WTF
(PYTHON)( )
Welcome To Functional
(PYTHON)( )
But my typing speed really got increased since using Java.
PARTIAL FUNCTION
APPLICATION
(((a × b) → c) × a) → (b → c)
(PYTHON)( )
PARTIAL FUNCTION
APPLICATION
from functools import partial
def f(x, y, z):
return x + y + z
f2 = partial(f, 10...
(PYTHON)( )
PARTIAL FUNCTION
APPLICATION
+ Keyword-based partial application
from functools import partial
from datetime i...
CURRYING
((a × b × c) → d) → (((a → b) → c) → d)
(PYTHON)( )
CURRYING
+ Simple sum
def simple_sum(a, b):
return sum(range(a, b+1))
>>> simple_sum(1, 10)
55
(PYTHON)( )
CURRYING
+ Squared sum
def square_sum(a, b):
return sum(map(lambda x: x**2, range(a,b+1)))
>>> square_sum(1,10...
(PYTHON)( )
CURRYING
+ Square root sum
def sqrt_sum(a, b):
return sum(map(math.sqrt, range(a,b+1)))
>>> sqrt_sum(1,10)
22....
(PYTHON)( )
CURRYING
+ Curried sum()
import math
def fsum(f):
def apply(a, b):
return sum(map(f, range(a,b+1)))
return app...
(PYTHON)( )
CURRYING
+ Combined with partial
def fsum(f):
def apply(a, b):
return sum(map(f, range(a,b+1)))
return apply
f...
(PYTHON)( )
DECORATOR
+ Another way to curry
import math
def fsum(f):
def apply(a, b):
return sum(map(f, range(a,b+1)))
re...
(PYTHON)( )
GENERIC CURRY IN PYTHON
import functools
def curry(func):
def curried(*args, **kwargs):
if not args and not kw...
(PYTHON)( )
TAIL RECURSION
+ No grammar-level support, just simulation
def printAll(strings):
if strings:
print strings[0]...
(PYTHON)( )
SO?
(PYTHON)( )
FUNCTIONAL PYTHON?
Pros
+ First-class function
+ lambda
+ built-in map/filter/reduce
+ functools
+ generators a...
(PYTHON)( )
CONCLUSION
+ Python is not for replacing any functional
language
+ But a good way to begin functional
programm...
(PYTHON)( )
THINK FUNCTIONAL.
(PYTHON)( )
ALL CODE SAMPLES ON GIST
(PYTHON)( )
END
Questions
Upcoming SlideShare
Loading in...5
×

Functional programming in Python

3,684

Published on

Published in: Technology, Education
1 Comment
24 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,684
On Slideshare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
63
Comments
1
Likes
24
Embeds 0
No embeds

No notes for slide

Functional programming in Python

  1. 1. Functional Programming in Python (PYTHON)( )
  2. 2. TODAY'S SLIDES http://goo.gl/iRXD0Y
  3. 3. (PYTHON)( ) ABOUT ME + Colin Su a.k.a LittleQ + http://about.me/littleq + Productional Experience: - Erlang - Python - Java{,Script}
  4. 4. (PYTHON)( ) PROGRAMMING IN PYTHON + Imperative Programming - Shell Scripting + Procedural Programming - Languages with functions + Declarative Programming - Functional Programming
  5. 5. (PYTHON)( ) FUNCTIONAL PROGRAMMING + No state + Immutable data + Function as first-class citizen + Higher-order function + Purity + Recursion, tail recursion + ...the list is too long to name here
  6. 6. (PYTHON)( ) IP VS FP $ ./program1 $ ./program2 --arg=1 $ ./program3 $ ./program1 | ./program2 --arg=1 | ./program3 Imperative Functional Your Terminal
  7. 7. (PYTHON)( ) FUNCTION AS FIRST CLASS OBJ def add(a, b): return a + b add2 = add add2(1,2) # 3 def giveMeAdd(): def add(a, b): return a + b return add
  8. 8. (PYTHON)( ) LAMBDA SUPPORT add = lambda a, b : a + b minus = lambda a, b : a - b multiply = lambda a, b : a * b divide = lambda a, b : a / b
  9. 9. (PYTHON)( ) (POOR) LAMBDA SUPPORT add = lambda a, b: c = a + b return c
  10. 10. (PYTHON)( ) (POOR) LAMBDA SUPPORT add = lambda a, b: c = a + b return c File "test.py", line 29 add = lambda a, b: ^ SyntaxError: invalid syntax
  11. 11. (PYTHON)( ) HIGHER-ORDER FUNCTION + Function eats functions, and returns functions def food(): return "food got eaten" drink = lambda: "drink got drunk" def me(*args): def eat(): return map(lambda x: x(), args) return eat act = me(food, drink) print act() >>> ['food got eaten', 'drink got drunk']
  12. 12. (PYTHON)( ) MAP/REDUCE/FILTER + A war between readability and conciseness + To make your code pluggable
  13. 13. (PYTHON)( ) CALCULATE "1++2+3+++4+5"
  14. 14. (PYTHON)( ) IMPERATIVE.PY INPUT = "1+2++3+++4++5+6+7++8+9++10" result = 0 for num in INPUT.split('+'): if num: result += int(num) print result Iterate on the same variable to perform task
  15. 15. (PYTHON)( ) FUNCTIONAL.PY from operator import add INPUT = "1+2++3+++4++5+6+7++8+9++10" print reduce(add, map(int, filter(bool, INPUT.split('+'))))
  16. 16. (PYTHON)( ) FUNCTIONAL PYTHON MADE SIMPLE
  17. 17. (PYTHON)( ) BUT NOT ALL LANGUAGES DO...
  18. 18. (PYTHON)( ) IMPERATIVE.JAVA Multiset<Integer> lengths = HashMultiset.create(); for (String string : strings) { if (CharMatcher.JAVA_UPPER_CASE.matchesAllOf(string)) { lengths.add(string.length()); } } Looks making sense
  19. 19. (PYTHON)( ) FUNCTIONAL.JAVA Function<String, Integer> lengthFunction = new Function<String, Integer>() { public Integer apply(String string) { return string.length(); } }; Predicate<String> allCaps = new Predicate<String>() { public boolean apply(String string) { return CharMatcher.JAVA_UPPER_CASE.matchesAllOf(string); } }; Multiset<Integer> lengths = HashMultiset.create( Iterables.transform(Iterables.filter(strings, allCaps), lengthFunction));
  20. 20. (PYTHON)( ) WTF
  21. 21. (PYTHON)( ) Welcome To Functional
  22. 22. (PYTHON)( ) But my typing speed really got increased since using Java.
  23. 23. PARTIAL FUNCTION APPLICATION (((a × b) → c) × a) → (b → c)
  24. 24. (PYTHON)( ) PARTIAL FUNCTION APPLICATION from functools import partial def f(x, y, z): return x + y + z f2 = partial(f, 100, 10) print f2(5) #115
  25. 25. (PYTHON)( ) PARTIAL FUNCTION APPLICATION + Keyword-based partial application from functools import partial from datetime import datetime def log(message, prefix="", postfix=""): print prefix, message, postfix error = partial(log, postfix=datetime.now(), prefix="[ERROR]") error("something goes wrong") # [ERROR] something goes wrong 2014-04-10 01:37:07.250509
  26. 26. CURRYING ((a × b × c) → d) → (((a → b) → c) → d)
  27. 27. (PYTHON)( ) CURRYING + Simple sum def simple_sum(a, b): return sum(range(a, b+1)) >>> simple_sum(1, 10) 55
  28. 28. (PYTHON)( ) CURRYING + Squared sum def square_sum(a, b): return sum(map(lambda x: x**2, range(a,b+1))) >>> square_sum(1,10) 385
  29. 29. (PYTHON)( ) CURRYING + Square root sum def sqrt_sum(a, b): return sum(map(math.sqrt, range(a,b+1))) >>> sqrt_sum(1,10) 22.4682781862041
  30. 30. (PYTHON)( ) CURRYING + Curried sum() import math def fsum(f): def apply(a, b): return sum(map(f, range(a,b+1))) return apply simple_sum = fsum(int) square_sum = fsum(lambda x: x**2) sqrt_sum = fsum(math.sqrt) print simple_sum(1,10) # 55 print square_sum(1,10) # 385 print sqrt_sum(1,10) # 22.4682781862
  31. 31. (PYTHON)( ) CURRYING + Combined with partial def fsum(f): def apply(a, b): return sum(map(f, range(a,b+1))) return apply from functools import partial from operator import mul mul_sum = fsum(partial(mul, 2)) print mul_sum(1,10) # 110
  32. 32. (PYTHON)( ) DECORATOR + Another way to curry import math def fsum(f): def apply(a, b): return sum(map(f, range(a,b+1))) return apply @fsum def sqrt_sum(x): return math.sqrt(x) print sqrt_sum(1,10) # 22.4682781862
  33. 33. (PYTHON)( ) GENERIC CURRY IN PYTHON import functools def curry(func): def curried(*args, **kwargs): if not args and not kwargs: return func() return curry(functools.partial(func, *args, **kwargs)) return curried @curry def add(a, b, c, d, e, f, g): return a + b + c + d + e + f + g add12 = add(1)(2) add1234 = add12(3)(4) add1234567 = add1234(5)(6)(7) print add1234567() # 28
  34. 34. (PYTHON)( ) TAIL RECURSION + No grammar-level support, just simulation def printAll(strings): if strings: print strings[0] return printAll(strings[1:]) printAll([1,2,3,4,5]) Python 2 def printAll(strings): if strings: head, *tail = strings print(head) return printAll(tail) printAll([1,2,3,4,5]) Python 3
  35. 35. (PYTHON)( ) SO?
  36. 36. (PYTHON)( ) FUNCTIONAL PYTHON? Pros + First-class function + lambda + built-in map/filter/reduce + functools + generators as lazy-evaluation Cons + non-pure + lambda (?) + memory-cost operations + No optimization for tail recursion + No pattern matching
  37. 37. (PYTHON)( ) CONCLUSION + Python is not for replacing any functional language + But a good way to begin functional programming
  38. 38. (PYTHON)( ) THINK FUNCTIONAL.
  39. 39. (PYTHON)( ) ALL CODE SAMPLES ON GIST
  40. 40. (PYTHON)( ) END Questions
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×