Mircea Bogdan, 5/21/041 Chicago TDC Design and Implementation Mircea Bogdan (UC)
Practical Functional Programming Presentation by Bogdan Hodorog
-
Upload
3pillar-global -
Category
Software
-
view
288 -
download
0
description
Transcript of Practical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming
Practical Functional Programming
Bogdan Hodorog
8th November 2014 / Functional Angle
Practical Functional Programming
Introduction
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
Introduction
About the speaker
linkedin.com says Technical Managermy heart says Software engineer
is daily reviewing and writing python codesometimes reads and changes elisp codeseldom reads c codepresently is trying to add golang to his cvin the far past he used java for several yearsrudimentary knowledge of ruby, javascript, ML
Practical Functional Programming
Introduction
About the speaker
linkedin.com says Technical Managermy heart says Software engineer
is daily reviewing and writing python codesometimes reads and changes elisp codeseldom reads c codepresently is trying to add golang to his cvin the far past he used java for several yearsrudimentary knowledge of ruby, javascript, ML
Practical Functional Programming
Introduction
What is the goal of this talk?
intended for programmers using non-strict functionalprogramming languagesuse something you learned from this presentation in thenext week while writing your usual code
Practical Functional Programming
First class functions
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
First class functions
First order functions
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
First class functions
First order functions
Context
A function’s components
a unique identifier, commonly refered as namean parameters listinstruction(s), commonly refered a bodyreturn value
Question?
What kind of arguments can be passed?
Answer
Any kind of objects except functions
Practical Functional Programming
First class functions
First order functions
Code
python
def name(par1, par2):val = "something"return val
elisp
(defun name(par1 par2)(let ((return_val "something"))
return_val))
Practical Functional Programming
First class functions
Higher order functions
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
First class functions
Higher order functions
Context
Question?
What kind of arguments can be passed?
Answer
any type including functionsfunctions must be first-class citizens
Practical Functional Programming
First class functions
Higher order functions
Context
python
def capitalize(names):result = []for name in names:
result.append(name.capitalize())return result
team = ["laura", "bogdan", "titus", "zoli", "alex"]
>>> capitalize(team)[’Laura’, ’Bogdan’, ’Titus’, ’Zoli’, ’Alex’]
Practical Functional Programming
First class functions
Higher order functions
Reason
python
def c a p i t a l i z e (names ) :r e s u l t = [ ]for name in names :
r e s u l t . append (name . c a p i t a l i z e ( ) )return r e s u l t
def uppercase (names ) :r e s u l t = [ ]for name in names :
r e s u l t . append (name . uppercase ( ) )return r e s u l t
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> c a p i t a l i z e ( team )[ ’ Laura ’ , ’ Bogdan ’ , ’ T i t us ’ , ’ Z o l i ’ , ’ Alex ’ ]>>> uppercase ( team )[ ’LAURA ’ , ’BOGDAN ’ , ’ TITUS ’ , ’ ZOLI ’ , ’ALEX ’ ]
Practical Functional Programming
First class functions
Higher order functions
Enter higher order function
python
def process (names , func ) :r e s u l t = [ ]for name in names :
r e s u l t . append ( func (name ) )return r e s u l t
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> process ( team , s t r i n g . c a p i t a l i z e )[ ’ Laura ’ , ’ Bogdan ’ , ’ T i t us ’ , ’ Z o l i ’ , ’ Alex ’ ]>>> process ( team , s t r i n g . upper )[ ’LAURA ’ , ’BOGDAN ’ , ’ TITUS ’ , ’ ZOLI ’ , ’ALEX ’ ]
Practical Functional Programming
First class functions
Higher order functions
Generic examples
python
def high_ord ( par1 , func ) :va l = " { } and { } " . format ( par1 , func ( par1 ) )return va l
high_ord ( " bogdan " , s t r i n g . c a p i t a l i z e )
elisp
( defun high_ord ( par1 func )( l e t ( ( r e t u r n _ v a l ( format "%s and %s "
par1 ( funcal l func par1 ) ) ) )r e t u r n _ v a l ) )
( h igh_ord " something " ( f u n c t i o n c a p i t a l i z e ) )
Practical Functional Programming
First class functions
Higher order functions
Decorators
Question
What about returning a function from a function?
python
def fo rb idden ( ) :va l = " I can ’ t change t h i s code "return va l
def decorate ( func ) :def wrapper ( ) :
pr in t " Something new I want to add "return func ( )
return wrapper
fo rb idden = decorate ( fo rb idden )
>>> pr in t fo rb idden ( )Something new I want to add" I can ’ t change t h i s code "
Practical Functional Programming
First class functions
map()/filter()/reduce()
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
First class functions
map()/filter()/reduce()
map()
python
def process (names , func ) :r e s u l t = [ ]for name in names :
r e s u l t . append ( func (name ) )return r e s u l t
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> process ( team , s t r i n g . c a p i t a l i z e )[ ’ Laura ’ , ’ Bogdan ’ , ’ T i t us ’ , ’ Z o l i ’ , ’ Alex ’ ]>>> process ( team , s t r i n g . upper )[ ’LAURA ’ , ’BOGDAN ’ , ’ TITUS ’ , ’ ZOLI ’ , ’ALEX ’ ]
Practical Functional Programming
First class functions
map()/filter()/reduce()
map()
python
def map( func , c o l l e c t i o n ) :r e s u l t = [ ]for elem in c o l l e c t i o n :
r e s u l t . append ( func ( elem ) )return r e s u l t
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> map( s t r i n g . c a p i t a l i z e , team )[ ’ Laura ’ , ’ Bogdan ’ , ’ T i t us ’ , ’ Z o l i ’ , ’ Alex ’ ]>>> process ( s t r i n g . upper , team )[ ’LAURA ’ , ’BOGDAN ’ , ’ TITUS ’ , ’ ZOLI ’ , ’ALEX ’ ]
Practical Functional Programming
First class functions
map()/filter()/reduce()
filter()
python
def g e t _ s t a r t s _ w i t h ( chars , names ) :r e s u l t = [ ]for name in names :
i f name [ 0 ] in chars :r e s u l t . append (name)
return r e s u l t
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> g e t _ s t a r t s _ w i t h ( " aeiou " , team )[ ’ a lex ’ ]
Practical Functional Programming
First class functions
map()/filter()/reduce()
filter()
python
def f i l t e r ( func , c o l l e c t i o n ) :r e s u l t = [ ]for name in names :
i f func (name ) :r e s u l t . append (name)
return r e s u l t
def s t a r t s _ w i t h ( e l ) :return e l [ 0 ] in " aeiou "
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> f i l t e r ( s t a r t s _ w i t h , team )[ ’ a lex ’ ]
Practical Functional Programming
First class functions
map()/filter()/reduce()
Alternative: enter List Comprehensions
python
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]>>> [ s t r i n g . upper (name) for name in team ][ ’LAURA ’ , ’BOGDAN ’ , ’ TITUS ’ , ’ ZOLI ’ , ’ALEX ’ ]>>> [ name for name in team i f name [ 0 ] in " aeiou " ][ ’ a lex ’ ]
Practical Functional Programming
First class functions
map()/filter()/reduce()
reduce()
Aliases
fold, accumulate, aggregate
python
def count_chars (names ) :r e s u l t = 0for name in names :
r e s u l t += len (name)return r e s u l t
team = [ ’ l au ra ’ , ’ bogdan ’ , ’ t i t u s ’ , ’ z o l i ’ , ’ a lex ’ ]>>> count_chars ( team )24
Practical Functional Programming
First class functions
map()/filter()/reduce()
reduce()
Aliases
fold, accumulate, aggregate
python
def accumulator ( acc , e l ) :return acc + len ( e l )
team = [ ’ l au ra ’ , ’ bogdan ’ , ’ t i t u s ’ , ’ z o l i ’ , ’ a lex ’ ]>>> reduce ( accumulator , team , 0)24
Practical Functional Programming
First class functions
map()/filter()/reduce()
Practicality
solution for reducing code nesting level
Practical Functional Programming
First class functions
map()/filter()/reduce()
Practicality
solution for reducing code nesting level
python
def f i l t e r ( func , c o l l e c t i o n ) :r e s u l t = [ ]for name in names :
i f func (name ) :r e s u l t . append (name)
return r e s u l t
python
def s t a r t s _ w i t h ( e l ) :return e l [ 0 ] in " aeiou "
f i l t e r ( s t a r t s _ w i t h , team )
Practical Functional Programming
First class functions
map()/filter()/reduce()
Practicality
solution for reducing code nesting leveluseful for oneliners
Practical Functional Programming
First class functions
map()/filter()/reduce()
Practicality
solution for reducing code nesting leveluseful for onelinerson the other hand, try to debug them :(
Practical Functional Programming
First class functions
map()/filter()/reduce()
Practicality
solution for reducing code nesting leveluseful for onelinerson the other hand, try to debug them :(map/filter apart of having aliases might be disguised inother forms (e.g. comprehensions)
Practical Functional Programming
First class functions
Annonymous functions
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
First class functions
Annonymous functions
lambda
the term comes from Lambda calculusoften refers to a programming language support forannonymous functionsoften has slightly different semantic in each language
python: anonymous function with a single expression asbodyelisp: anonymous function without a name
Practical Functional Programming
First class functions
Closures
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
First class functions
Closures
Closures
Explained
function + environmentenvironment contains local variables, paramsshort of lexical closure as opossed to dynamic scopingfunction’s variables bound where defined not where calledfavour laziness, they’re active only when called
Practical Functional Programming
First class functions
Closures
Lexical scope, not dynamic!
python
def outer ( ) :global apr in t " g loba l environment : a = { } " . format ( a )
def i nne r ( b ) :pr in t " i nne r environment : a = { } , b = { } " . format ( a , b )return a + b
a = 2pr in t ( " ou ter environment : a = { } " . format ( a ) )pr in t i nne r ( a+2)
a = 10pr in t " r e s u l t i s { } " . format ( ou ter ( ) )
valid python, lexical scope
>>> global environment : a=10outer environment : a=2inner environment : a=2 ,b=4r e s u l t is 6
Practical Functional Programming
First class functions
Closures
Lexical scope, not dynamic!
python
def outer ( ) :global apr in t " g loba l environment : a = { } " . format ( a )
def i nne r ( b ) :pr in t " i nne r environment : a = { } , b = { } " . format ( a , b )return a + b
a = 2pr in t ( " ou ter environment : a = { } " . format ( a ) )pr in t i nne r ( a+2)
a = 10pr in t " r e s u l t i s { } " . format ( ou ter ( ) )
imaginary dynamic scope
>>> global environment : a=10outer environment : a=2inner environment : a=10 ,b=4r e s u l t is 14
Practical Functional Programming
First class functions
Closures
Practicality
use them for lazinessuse them for memoizationuse them to have more expressive codethink of them as alternative to classesknow your language
most languages use just lexical scopingsome languages allows switching between lexical anddynamic scopingclosures for functions/lambdas/others may behave different
Practical Functional Programming
First class functions
Partial application
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
First class functions
Partial application
Remember filter() example
python
def f i l t e r ( func , c o l l e c t i o n ) :r e s u l t = [ ]for name in names :
i f func (name ) :r e s u l t . append (name)
return r e s u l t
Refactor starts_with
def s t a r t s _ w i t h ( e l ) :return e l [ 0 ] in " aeiou "
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> f i l t e r ( s t a r t s _ w i t h , team )[ ’ a lex ’ ]
Practical Functional Programming
First class functions
Partial application
The problem
python
def f i l t e r ( func , c o l l e c t i o n ) :r e s u l t = [ ]for name in names :
i f func (name ) :r e s u l t . append (name)
return r e s u l t
How do we pass the 2nd arg?
def s t a r t s _ w i t h ( e l , p r e f i x ) :return e l [ 0 ] in p r e f i x
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> f i l t e r ( s t a r t s _ w i t h , team )TypeError : s t a r t s _ w i t h ( ) takes exac t l y 2 arguments (1 given )
Practical Functional Programming
First class functions
Partial application
Solution
python
def s t a r t s _ w i t h ( e l , p r e f i x ) :return e l [ 0 ] in p r e f i x
import f u nc t o o l sf rozen_func = f u n c to o l s . p a r t i a l ( s t a r t s _ w i t h , p r e f i x = " aeiou " )team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> f i l t e r ( f rozen_func , team )[ ’ a lex ’ ]>>> f rozen_func . func< f u n c t i o n s t a r t s _ w i t h a t 0x103b4c500>>>> f rozen_func . keywords{ ’ p r e f i x ’ : ’ aeiou ’ }
Explained
curried("aeiou") is a function object waiting to be calledits closure contains variable prefix="aeiou"
Practical Functional Programming
First class functions
Partial application
Solution
python
def s t a r t s _ w i t h ( e l , p r e f i x ) :return e l [ 0 ] in p r e f i x
import f u nc t o o l sf rozen_func = f u n c to o l s . p a r t i a l ( s t a r t s _ w i t h , p r e f i x = " aeiou " )team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> f i l t e r ( f rozen_func , team )[ ’ a lex ’ ]>>> f rozen_func . func< f u n c t i o n s t a r t s _ w i t h a t 0x103b4c500>>>> f rozen_func . keywords{ ’ p r e f i x ’ : ’ aeiou ’ }
Explained
curried("aeiou") is a function object waiting to be calledits closure contains variable prefix="aeiou"
Practical Functional Programming
First class functions
Partial application
Practicality
work with map/filter functionsenforce some client code based on higher order functionsto use fixed value for some argumentsoften can be replace with lambda
python
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> f i l t e r ( lambda e l : s t a r t s _ w i t h ( e l , " aeiou " ) , team )[ ’ a lex ’ ]
Practical Functional Programming
First class functions
Currying
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
First class functions
Currying
Origin
named after Haskell Curry(mathematician), not the dish!Lambda calculus theory: says eachfunction has a single paramHow are multi-params function emulatedusing this paradigm?Using function composition
Practical Functional Programming
First class functions
Currying
Origin
named after Haskell Curry(mathematician), not the dish!Lambda calculus theory: says eachfunction has a single paramHow are multi-params function emulatedusing this paradigm?Using function composition
Practical Functional Programming
First class functions
Currying
Origin
named after Haskell Curry(mathematician), not the dish!Lambda calculus theory: says eachfunction has a single paramHow are multi-params function emulatedusing this paradigm?Using function composition
Practical Functional Programming
First class functions
Currying
Origin
named after Haskell Curry(mathematician), not the dish!Lambda calculus theory: says eachfunction has a single paramHow are multi-params function emulatedusing this paradigm?Using function composition
Practical Functional Programming
First class functions
Currying
Origin
named after Haskell Curry(mathematician), not the dish!Lambda calculus theory: says eachfunction has a single paramHow are multi-params function emulatedusing this paradigm?Using function composition
Practical Functional Programming
First class functions
Currying
Problem
same as for Partial application
How do we pass the 2nd arg?
def s t a r t s _ w i t h ( e l , p r e f i x ) :return e l [ 0 ] in p r e f i x
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> f i l t e r ( s t a r t s _ w i t h , team )TypeError : s t a r t s _ w i t h ( ) takes exac t l y 2 arguments (1 given )
Practical Functional Programming
First class functions
Currying
Solution
python
def c u r r i e d ( p r e f i x ) :def e l_c losu re ( e l ) :
return e l [ 0 ] in p r e f i xreturn e l_c losu re
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
>>> f i l t e r ( c u r r i e d ( " aeiou " ) , team )[ ’ a lex ’ ]
Practical Functional Programming
First class functions
Currying
Solution
python
def m u l t i p l e ( a , b , c ) :return a + b + c
def a_f ( a ) :def b_f ( b ) :
def c_f ( c ) :return a + b + c
return c_freturn b_f
m u l t i p l e (1 , 2 , 3)a_f ( 1 ) ( 2 ) ( 3 )" " "a_f ( 1 ) ( 2 ) ( 3 )
b_f ( 2 ) ( 3 )c_f ( 3 )
" " "
SML
fun foo ( a : i n t , b : i n t , c : i n t ) =a + b +c
val c u r r i e d = fn x =>fn y => fn z => x + y + z
− ( ( ( c u r r i e d 1) 2) 3 ) ;6− c u r r i e d 1 2 3;6
Practical Functional Programming
First class functions
Currying
Practicality
not used in daily situationscame accross it in some technical docs
Practical Functional Programming
First class functions
Currying
vs Partial application
both rely on closureboth solve the same problemssubtle difference
currying returns a function object with single parampartial application may return a function object with multipleparams
Practical Functional Programming
Immutability
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
Immutability
Context
Question
Why should we care about types being mutable or immutable?
python
def reorder(coll) --> a sorted collection
team = ["laura", "bogdan", "titus", "zoli", "alex"]>>> print reorder(team)[’alex’, ’bogdan’, ’laura’, ’titus’, ’zoli’]>>>
Practical Functional Programming
Immutability
Context
Question
Why should we care about types being mutable or immutable?
python
def reorder(coll) --> a sorted collection
team = ["laura", "bogdan", "titus", "zoli", "alex"]>>> print reorder(team)[’alex’, ’bogdan’, ’laura’, ’titus’, ’zoli’]>>> print team???
Practical Functional Programming
Immutability
Context
Question
Why should we care about types being mutable or immutable?
python
team = ["laura", "bogdan", "titus", "zoli", "alex"]def reorder(coll):
coll.sort()return coll
>>> reorder(team)>>> print team[’laura’, ’bogdan’, ’titus’, ’zoli’, ’alex’]
Practical Functional Programming
Immutability
Context
Question
Why should we care about types being mutable or immutable?
python
team = ["laura", "bogdan", "titus", "zoli", "alex"]def reorder(coll):
return sorted(coll)
>>> reorder(team)>>> print team["laura", "bogdan", "titus", "zoli", "alex"]
Practical Functional Programming
Immutability
Practicality
mutablity may lead to race conditionsimmutable objects are considered thread-safeunwanted modifications can be performed on sensitivecollectionspure functions doesn’t leak side effectssome languages support explicit syntax for mutableoperations
ruby’s bang .methods!scala’s var and val
Practical Functional Programming
Immutability
Practicality
Practical Functional Programming
Evaluation strategies
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
Evaluation strategies
Functional languages
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
Evaluation strategies
Functional languages
Eager vs Lazy
many of non-strict functional languages use eager(strict)evaluation
pass-by-valuepass-by-referencepass-by-sharing
most strict functional languages use lazy evaluation
Practical Functional Programming
Evaluation strategies
Non-functional languages
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
Evaluation strategies
Non-functional languages
Thunk
Reason
python
def crazy_complex ( i ) :pr in t " This i s O( n∗n∗n ) "return i ∗ 1
def add_some ( x , some ) :i f some > 0:
return x + 3return some
>>> pr in t add_some ( crazy_complex ( 8 ) , 1)This is O( n∗n∗n )11
Practical Functional Programming
Evaluation strategies
Non-functional languages
Thunk
Call by name
Evaluate the argument only if needed
python
def crazy_complex ( i ) :pr in t " This i s O( n∗n∗n ) "return i ∗ 1
def add_some ( thunk , some ) :i f some > 0:
return thunk ( ) + 3return some
from f u nc t o o l s import p a r t i a l # or use c u r r y i ng ins teadlazy_thunk = p a r t i a l ( crazy_complex , 8)
>>> pr in t add_some ( lazy_thunk , 1)This is O( n∗n∗n )11>>> pr in t add_some ( lazy_thunk , −1)−1
Practical Functional Programming
Evaluation strategies
Non-functional languages
Generators
Description
is a function object behaving like an iteratorgenerates one element at a time
python
import random
team = [ " lau ra " , " bogdan " , " t i t u s " , " z o l i " , " a lex " ]
def i n i f i n i t e ( c o l l ) :while True :
y i e l d random . choice ( team )
>>> gener = i n i f i n i t e ( team )
Practical Functional Programming
Evaluation strategies
Non-functional languages
Generators
Description
is a function object behaving like an iteratorgenerates one element at a time
python
>>> gener . next ( )’ bogdan ’>>> gener . next ( )’ l au ra ’>>> gener . next ( )’ z o l i ’
python
>>> gener . next ( )’ t i t u s ’>>> gener . next ( )’ z o l i ’>>> gener . next ( )’ l au ra ’
Practical Functional Programming
Evaluation strategies
Non-functional languages
Practicality
use thunks to decorate functionsthunks can help with lazy evaluationgenerators offers one element at a time, useful for dealingwith streamsuse generators for inifinte source of data
Practical Functional Programming
Uncommon topics
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
Uncommon topics
to look into
Tail call optimization: famous topic, butrarely encountered in practice (Scheme,ML, Erlang)Pattern matching
Practical Functional Programming
Remember checklist
Outline1 Introduction2 First class functions
First order functionsHigher order functionsmap()/filter()/reduce()Annonymous functionsClosuresPartial applicationCurrying
3 Immutability4 Evaluation strategies
Functional languagesNon-functional languages
5 Uncommon topics6 Remember checklist
Practical Functional Programming
Remember checklist
Asses functional capabilities of a programminglanguage
1 check for first class functions: pass, bound and return2 look for map/filter/reduce/comprehension and beware of
when to use them3 support for closure4 annonymous functions and their limit5 identify immutable types and other related features6 understand the evaluation strategies employed by the
language
Practical Functional Programming
Remember checklist