SlideShare a Scribd company logo
1 of 74
Download to read offline
Practical Functional Programming 
Practical Functional Programming 
Bogdan Hodorog 
8th November 2014 / Functional Angle
Practical Functional Programming 
Introduction 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
Introduction 
About the speaker 
linkedin.com says Technical Manager 
my heart says Software engineer 
is daily reviewing and writing python code 
sometimes reads and changes elisp code 
seldom reads c code 
presently is trying to add golang to his cv 
in the far past he used java for several years 
rudimentary knowledge of ruby, javascript, ML
Practical Functional Programming 
Introduction 
About the speaker 
linkedin.com says Technical Manager 
my heart says Software engineer 
is daily reviewing and writing python code 
sometimes reads and changes elisp code 
seldom reads c code 
presently is trying to add golang to his cv 
in the far past he used java for several years 
rudimentary knowledge of ruby, javascript, ML
Practical Functional Programming 
Introduction 
What is the goal of this talk? 
intended for programmers using non-strict functional 
programming languages 
use something you learned from this presentation in the 
next week while writing your usual code
Practical Functional Programming 
First class functions 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
First class functions 
First order functions 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
First class functions 
First order functions 
Context 
A function’s components 
a unique identifier, commonly refered as name 
an parameters list 
instruction(s), commonly refered a body 
return 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 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
First class functions 
Higher order functions 
Context 
Question? 
What kind of arguments can be passed? 
Answer 
any type including functions 
functions 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> c a p i t a l i z e ( team) 
[ ’ Laura ’ , ’Bogdan ’ , ’ Ti tu s ’ , ’ 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> process ( team , s t r i n g . c a p i t a l i z e ) 
[ ’ Laura ’ , ’Bogdan ’ , ’ Ti tu s ’ , ’ 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 ) : 
val = " { } and { } " . format ( par1 , func ( par1 ) ) 
return val 
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 ) ) 
( high_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 forbidden ( ) : 
val = " I can ’ t change t h i s code " 
return val 
def decorate ( func ) : 
def wrapper ( ) : 
pr int " Something new I want to add " 
return func ( ) 
return wrapper 
forbidden = decorate ( forbidden ) 
>>> pr int forbidden ( ) 
Something new I want to add 
" I can ’ t change t h i s code "
Practical Functional Programming 
First class functions 
map()/filter()/reduce() 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> process ( team , s t r i n g . c a p i t a l i z e ) 
[ ’ Laura ’ , ’Bogdan ’ , ’ Ti tu s ’ , ’ 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> map( s t r i n g . c a p i t a l i z e , team) 
[ ’ Laura ’ , ’Bogdan ’ , ’ Ti tu s ’ , ’ 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 _wi 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> g e t _ s t a r t s _wi t h ( " aeiou " , team) 
[ ’ alex ’ ]
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 _wi t h ( e l ) : 
return e l [ 0 ] in " aeiou " 
team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> f i l t e r ( s t a r t s _wi t h , team) 
[ ’ alex ’ ]
Practical Functional Programming 
First class functions 
map()/filter()/reduce() 
Alternative: enter List Comprehensions 
python 
team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> [ 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 " ] 
[ ’ alex ’ ]
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 = 0 
for name in names : 
r e s u l t += len (name) 
return r e s u l t 
team = [ ’ laura ’ , ’ bogdan ’ , ’ t i t u s ’ , ’ z o l i ’ , ’ alex ’ ] 
>>> 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 = [ ’ laura ’ , ’ bogdan ’ , ’ t i t u s ’ , ’ z o l i ’ , ’ alex ’ ] 
>>> 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 _wi t h ( e l ) : 
return e l [ 0 ] in " aeiou " 
f i l t e r ( s t a r t s _wi t h , team)
Practical Functional Programming 
First class functions 
map()/filter()/reduce() 
Practicality 
solution for reducing code nesting level 
useful for oneliners
Practical Functional Programming 
First class functions 
map()/filter()/reduce() 
Practicality 
solution for reducing code nesting level 
useful for oneliners 
on the other hand, try to debug them :(
Practical Functional Programming 
First class functions 
map()/filter()/reduce() 
Practicality 
solution for reducing code nesting level 
useful for oneliners 
on the other hand, try to debug them :( 
map/filter apart of having aliases might be disguised in 
other forms (e.g. comprehensions)
Practical Functional Programming 
First class functions 
Annonymous functions 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
First class functions 
Annonymous functions 
lambda 
the term comes from Lambda calculus 
often refers to a programming language support for 
annonymous functions 
often has slightly different semantic in each language 
python: anonymous function with a single expression as 
body 
elisp: anonymous function without a name
Practical Functional Programming 
First class functions 
Closures 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
First class functions 
Closures 
Closures 
Explained 
function + environment 
environment contains local variables, params 
short of lexical closure as opossed to dynamic scoping 
function’s variables bound where defined not where called 
favour laziness, they’re active only when called
Practical Functional Programming 
First class functions 
Closures 
Lexical scope, not dynamic! 
python 
def outer ( ) : 
global a 
pr int " global environment : a ={ } " . format ( a ) 
def inner ( b ) : 
pr int " inner environment : a ={ } , b ={ } " . format ( a , b ) 
return a + b 
a = 2 
pr int ( " outer environment : a ={ } " . format ( a ) ) 
pr int inner ( a+2) 
a = 10 
pr int " r e s u l t i s { } " . format ( outer ( ) ) 
valid python, lexical scope 
>>> global environment : a=10 
outer environment : a=2 
inner environment : a=2 ,b=4 
r e s u l t is 6
Practical Functional Programming 
First class functions 
Closures 
Lexical scope, not dynamic! 
python 
def outer ( ) : 
global a 
pr int " global environment : a ={ } " . format ( a ) 
def inner ( b ) : 
pr int " inner environment : a ={ } , b ={ } " . format ( a , b ) 
return a + b 
a = 2 
pr int ( " outer environment : a ={ } " . format ( a ) ) 
pr int inner ( a+2) 
a = 10 
pr int " r e s u l t i s { } " . format ( outer ( ) ) 
imaginary dynamic scope 
>>> global environment : a=10 
outer environment : a=2 
inner environment : a=10 ,b=4 
r e s u l t is 14
Practical Functional Programming 
First class functions 
Closures 
Practicality 
use them for laziness 
use them for memoization 
use them to have more expressive code 
think of them as alternative to classes 
know your language 
most languages use just lexical scoping 
some languages allows switching between lexical and 
dynamic scoping 
closures for functions/lambdas/others may behave different
Practical Functional Programming 
First class functions 
Partial application 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 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 _wi t h ( e l ) : 
return e l [ 0 ] in " aeiou " 
team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> f i l t e r ( s t a r t s _wi t h , team) 
[ ’ alex ’ ]
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 _wi t h ( el , p r e f i x ) : 
return e l [ 0 ] in p r e f i x 
team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> f i l t e r ( s t a r t s _wi t h , team) 
TypeError : s t a r t s _wi 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 _wi t h ( el , p r e f i x ) : 
return e l [ 0 ] in p r e f i x 
import f u n c t o o l s 
f rozen_func = f u n c t o o l s . p a r t i a l ( s t a r t s _wi t h , p r e f i x =" aeiou " ) 
team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> f i l t e r ( f rozen_func , team) 
[ ’ alex ’ ] 
>>> f rozen_func . func 
< f u n c t i o n s t a r t s _wi t h at 0x103b4c500> 
>>> f rozen_func . keywords 
{ ’ p r e f i x ’ : ’ aeiou ’ }
Practical Functional Programming 
First class functions 
Partial application 
Solution 
python 
def s t a r t s _wi t h ( el , p r e f i x ) : 
return e l [ 0 ] in p r e f i x 
import f u n c t o o l s 
f rozen_func = f u n c t o o l s . p a r t i a l ( s t a r t s _wi t h , p r e f i x =" aeiou " ) 
team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> f i l t e r ( f rozen_func , team) 
[ ’ alex ’ ] 
>>> f rozen_func . func 
< f u n c t i o n s t a r t s _wi t h at 0x103b4c500> 
>>> f rozen_func . keywords 
{ ’ p r e f i x ’ : ’ aeiou ’ } 
Explained 
curried("aeiou") is a function object waiting to be called 
its closure contains variable prefix="aeiou"
Practical Functional Programming 
First class functions 
Partial application 
Practicality 
work with map/filter functions 
enforce some client code based on higher order functions 
to use fixed value for some arguments 
often can be replace with lambda 
python 
team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> f i l t e r ( lambda e l : s t a r t s _wi t h ( el , " aeiou " ) , team) 
[ ’ alex ’ ]
Practical Functional Programming 
First class functions 
Currying 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
First class functions 
Currying 
Origin 
named after Haskell Curry 
(mathematician), not the dish! 
Lambda calculus theory: says each 
function has a single param 
How are multi-params function emulated 
using 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 each 
function has a single param 
How are multi-params function emulated 
using 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 each 
function has a single param 
How are multi-params function emulated 
using 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 each 
function has a single param 
How are multi-params function emulated 
using 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 each 
function has a single param 
How are multi-params function emulated 
using 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 _wi t h ( el , p r e f i x ) : 
return e l [ 0 ] in p r e f i x 
team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> f i l t e r ( s t a r t s _wi t h , team) 
TypeError : s t a r t s _wi 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 el_c losure ( e l ) : 
return e l [ 0 ] in p r e f i x 
return el_c losure 
team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] 
>>> f i l t e r ( c u r r i e d ( " aeiou " ) , team) 
[ ’ alex ’ ]
Practical Functional Programming 
First class functions 
Currying 
Solution 
python 
def mu 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_f 
return b_f 
mu 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 situations 
came accross it in some technical docs
Practical Functional Programming 
First class functions 
Currying 
vs Partial application 
both rely on closure 
both solve the same problems 
subtle difference 
currying returns a function object with single param 
partial application may return a function object with multiple 
params
Practical Functional Programming 
Immutability 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 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 conditions 
immutable objects are considered thread-safe 
unwanted modifications can be performed on sensitive 
collections 
pure functions doesn’t leak side effects 
some languages support explicit syntax for mutable 
operations 
ruby’s bang .methods! 
scala’s var and val
Practical Functional Programming 
Immutability 
Practicality
Practical Functional Programming 
Evaluation strategies 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
Evaluation strategies 
Functional languages 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
Evaluation strategies 
Functional languages 
Eager vs Lazy 
many of non-strict functional languages use eager(strict) 
evaluation 
pass-by-value 
pass-by-reference 
pass-by-sharing 
most strict functional languages use lazy evaluation
Practical Functional Programming 
Evaluation strategies 
Non-functional languages 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
Evaluation strategies 
Non-functional languages 
Thunk 
Reason 
python 
def crazy_complex ( i ) : 
pr int  This i s O( nnn )  
return i  1 
def add_some ( x , some ) : 
i f some  0: 
return x + 3 
return some 
 pr int add_some ( crazy_complex ( 8 ) , 1) 
This is O( nnn ) 
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 int  This i s O( nnn )  
return i  1 
def add_some ( thunk , some ) : 
i f some  0: 
return thunk ( ) + 3 
return some 
from f u n c t o o l s import p a r t i a l # or use cur r y i ng instead 
lazy_thunk = p a r t i a l ( crazy_complex , 8) 
 pr int add_some ( lazy_thunk , 1) 
This is O( nnn ) 
11 
 pr int add_some ( lazy_thunk , 1) 
1
Practical Functional Programming 
Evaluation strategies 
Non-functional languages 
Generators 
Description 
is a function object behaving like an iterator 
generates one element at a time 
python 
import random 
team = [  laura  ,  bogdan  ,  t i t u s  ,  z o l i  ,  alex  ] 
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 iterator 
generates one element at a time 
python 
 gener . next ( ) 
’ bogdan ’ 
 gener . next ( ) 
’ laura ’ 
 gener . next ( ) 
’ z o l i ’ 
python 
 gener . next ( ) 
’ t i t u s ’ 
 gener . next ( ) 
’ z o l i ’ 
 gener . next ( ) 
’ laura ’
Practical Functional Programming 
Evaluation strategies 
Non-functional languages 
Practicality 
use thunks to decorate functions 
thunks can help with lazy evaluation 
generators offers one element at a time, useful for dealing 
with streams 
use generators for inifinte source of data
Practical Functional Programming 
Uncommon topics 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
Uncommon topics 
to look into 
Tail call optimization: famous topic, but 
rarely encountered in practice (Scheme, 
ML, Erlang) 
Pattern matching
Practical Functional Programming 
Remember checklist 
Outline 
1 Introduction 
2 First class functions 
First order functions 
Higher order functions 
map()/filter()/reduce() 
Annonymous functions 
Closures 
Partial application 
Currying 
3 Immutability 
4 Evaluation strategies 
Functional languages 
Non-functional languages 
5 Uncommon topics 
6 Remember checklist
Practical Functional Programming 
Remember checklist 
Asses functional capabilities of a programming 
language 
1 check for first class functions: pass, bound and return 
2 look for map/filter/reduce/comprehension and beware of 
when to use them 
3 support for closure 
4 annonymous functions and their limit 
5 identify immutable types and other related features 
6 understand the evaluation strategies employed by the 
language
Practical Functional Programming 
Remember checklist

More Related Content

What's hot

Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekyoavrubin
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScriptWebF
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programmingAssaf Gannon
 
Functional programming in JavaScript
Functional programming in JavaScriptFunctional programming in JavaScript
Functional programming in JavaScriptJoseph Smith
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programmingAssaf Gannon
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardMario Fusco
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Chris Richardson
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming FundamentalsShahriar Hyder
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Vadim Dubs
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in ScalaDamian Jureczko
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With ScalaKnoldus Inc.
 

What's hot (20)

Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
 
Functional Programming with JavaScript
Functional Programming with JavaScriptFunctional Programming with JavaScript
Functional Programming with JavaScript
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
 
Haskell
HaskellHaskell
Haskell
 
Functional programming in JavaScript
Functional programming in JavaScriptFunctional programming in JavaScript
Functional programming in JavaScript
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
Monadic Java
Monadic JavaMonadic Java
Monadic Java
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Scala functions
Scala functionsScala functions
Scala functions
 

Viewers also liked

Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Scott Wlaschin
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian DragosScala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian Dragos3Pillar Global
 
Functional programming for optimization problems in Big Data
Functional programming for optimization problems in Big DataFunctional programming for optimization problems in Big Data
Functional programming for optimization problems in Big DataPaco Nathan
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional ProgrammingAndreas Pauley
 
Functional Programming Principles & Patterns
Functional Programming Principles & PatternsFunctional Programming Principles & Patterns
Functional Programming Principles & Patternszupzup.org
 
那些 Functional Programming 教我的事
那些 Functional Programming 教我的事那些 Functional Programming 教我的事
那些 Functional Programming 教我的事Wen-Tien Chang
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Scott Wlaschin
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 
DevOps for Windows Admins
DevOps for Windows Admins DevOps for Windows Admins
DevOps for Windows Admins Rex Antony Peter
 
Prototyping for Business Outcomes at ModevUX
Prototyping for Business Outcomes at ModevUXPrototyping for Business Outcomes at ModevUX
Prototyping for Business Outcomes at ModevUX3Pillar Global
 
Patrick Seguin Experience
Patrick Seguin ExperiencePatrick Seguin Experience
Patrick Seguin ExperiencePatrickSeguin
 
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...3Pillar Global
 
Automated Performance Testing for Desktop Applications by Ciprian Balea
Automated Performance Testing for Desktop Applications by Ciprian BaleaAutomated Performance Testing for Desktop Applications by Ciprian Balea
Automated Performance Testing for Desktop Applications by Ciprian Balea3Pillar Global
 
Psychological Issues and the law
Psychological Issues and the lawPsychological Issues and the law
Psychological Issues and the lawDoug Aaron
 
How to Avoid Getting Malware on your Computer
How to Avoid Getting Malware on your ComputerHow to Avoid Getting Malware on your Computer
How to Avoid Getting Malware on your ComputerJillian Stone
 
A Prototyping Case Study at NoVA UX
A Prototyping Case Study at NoVA UX A Prototyping Case Study at NoVA UX
A Prototyping Case Study at NoVA UX 3Pillar Global
 
Top 10 reasons to switch to the Nokia Lumia 1520
Top 10 reasons to switch to the Nokia Lumia 1520Top 10 reasons to switch to the Nokia Lumia 1520
Top 10 reasons to switch to the Nokia Lumia 1520Antony Worsley
 
MoDev East 2012 Presentation on Product Modernization
MoDev East 2012 Presentation on Product ModernizationMoDev East 2012 Presentation on Product Modernization
MoDev East 2012 Presentation on Product Modernization3Pillar Global
 

Viewers also liked (20)

Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Scala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian DragosScala: Object-Oriented Meets Functional, by Iulian Dragos
Scala: Object-Oriented Meets Functional, by Iulian Dragos
 
Functional programming for optimization problems in Big Data
Functional programming for optimization problems in Big DataFunctional programming for optimization problems in Big Data
Functional programming for optimization problems in Big Data
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Functional Programming Principles & Patterns
Functional Programming Principles & PatternsFunctional Programming Principles & Patterns
Functional Programming Principles & Patterns
 
那些 Functional Programming 教我的事
那些 Functional Programming 教我的事那些 Functional Programming 教我的事
那些 Functional Programming 教我的事
 
Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)Functional Programming Patterns (NDC London 2014)
Functional Programming Patterns (NDC London 2014)
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
DevOps for Windows Admins
DevOps for Windows Admins DevOps for Windows Admins
DevOps for Windows Admins
 
Prototyping for Business Outcomes at ModevUX
Prototyping for Business Outcomes at ModevUXPrototyping for Business Outcomes at ModevUX
Prototyping for Business Outcomes at ModevUX
 
Patrick Seguin Experience
Patrick Seguin ExperiencePatrick Seguin Experience
Patrick Seguin Experience
 
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
Three Things a New Product Team Needs - Jessica Hall's Presentation at the Bu...
 
KONSTANTINOS' EXPERIENCE
KONSTANTINOS' EXPERIENCEKONSTANTINOS' EXPERIENCE
KONSTANTINOS' EXPERIENCE
 
Automated Performance Testing for Desktop Applications by Ciprian Balea
Automated Performance Testing for Desktop Applications by Ciprian BaleaAutomated Performance Testing for Desktop Applications by Ciprian Balea
Automated Performance Testing for Desktop Applications by Ciprian Balea
 
Psychological Issues and the law
Psychological Issues and the lawPsychological Issues and the law
Psychological Issues and the law
 
How to Avoid Getting Malware on your Computer
How to Avoid Getting Malware on your ComputerHow to Avoid Getting Malware on your Computer
How to Avoid Getting Malware on your Computer
 
Cv 201503 eng_short
Cv 201503 eng_shortCv 201503 eng_short
Cv 201503 eng_short
 
A Prototyping Case Study at NoVA UX
A Prototyping Case Study at NoVA UX A Prototyping Case Study at NoVA UX
A Prototyping Case Study at NoVA UX
 
Top 10 reasons to switch to the Nokia Lumia 1520
Top 10 reasons to switch to the Nokia Lumia 1520Top 10 reasons to switch to the Nokia Lumia 1520
Top 10 reasons to switch to the Nokia Lumia 1520
 
MoDev East 2012 Presentation on Product Modernization
MoDev East 2012 Presentation on Product ModernizationMoDev East 2012 Presentation on Product Modernization
MoDev East 2012 Presentation on Product Modernization
 

Similar to Practical Functional Programming Presentation by Bogdan Hodorog

Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?UFPA
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slidejonycse
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programmingLukasz Dynowski
 
FP in scalaで鍛える関数型脳
FP in scalaで鍛える関数型脳FP in scalaで鍛える関数型脳
FP in scalaで鍛える関数型脳Yuri Inoue
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202Mahmoud Samir Fayed
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingIstanbul Tech Talks
 
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196Mahmoud Samir Fayed
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldWerner Hofstra
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, pythonRobert Lujo
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programaciónSoftware Guru
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Qiangning Hong
 
Object Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonObject Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonPython Ireland
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.pptssuserd64918
 
Python 培训讲义
Python 培训讲义Python 培训讲义
Python 培训讲义leejd
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Introthnetos
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1Hang Zhao
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 

Similar to Practical Functional Programming Presentation by Bogdan Hodorog (20)

Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
FP in scalaで鍛える関数型脳
FP in scalaで鍛える関数型脳FP in scalaで鍛える関数型脳
FP in scalaで鍛える関数型脳
 
The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
 
The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196The Ring programming language version 1.7 book - Part 35 of 196
The Ring programming language version 1.7 book - Part 35 of 196
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
 
Ejercicios de estilo en la programación
Ejercicios de estilo en la programaciónEjercicios de estilo en la programación
Ejercicios de estilo en la programación
 
Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010Python于Web 2.0网站的应用 - QCon Beijing 2010
Python于Web 2.0网站的应用 - QCon Beijing 2010
 
Object Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in PythonObject Orientation vs. Functional Programming in Python
Object Orientation vs. Functional Programming in Python
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
 
Scala
ScalaScala
Scala
 
Python 培训讲义
Python 培训讲义Python 培训讲义
Python 培训讲义
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 

More from 3Pillar Global

Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...3Pillar Global
 
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...3Pillar Global
 
Design Sprints Presentation at NoVA UX
Design Sprints Presentation at NoVA UX Design Sprints Presentation at NoVA UX
Design Sprints Presentation at NoVA UX 3Pillar Global
 
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...3Pillar Global
 
Less, But Better - Dieter Rams' Principles of Good Design
Less, But Better - Dieter Rams' Principles of Good DesignLess, But Better - Dieter Rams' Principles of Good Design
Less, But Better - Dieter Rams' Principles of Good Design3Pillar Global
 
Prototyping Your Way to Better and Faster Outcomes
Prototyping Your Way to Better and Faster Outcomes Prototyping Your Way to Better and Faster Outcomes
Prototyping Your Way to Better and Faster Outcomes 3Pillar Global
 
Cloud Platforms for Java
Cloud Platforms for JavaCloud Platforms for Java
Cloud Platforms for Java3Pillar Global
 
Visualizing Relationships: Journalistic Problems in a Digital Age
Visualizing Relationships: Journalistic Problems in a Digital AgeVisualizing Relationships: Journalistic Problems in a Digital Age
Visualizing Relationships: Journalistic Problems in a Digital Age3Pillar Global
 
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...3Pillar Global
 

More from 3Pillar Global (10)

Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
Great Ideas Don't Always Make Great Products - Jonathan Rivers' Presentation ...
 
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
The Five Stages of Accepting Negative Customer Feedback - Jessica Hall's Pres...
 
Design Sprints Presentation at NoVA UX
Design Sprints Presentation at NoVA UX Design Sprints Presentation at NoVA UX
Design Sprints Presentation at NoVA UX
 
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
Using Prototypes to Validate Product Strategy - Product Camp DC Presentation ...
 
Less, But Better - Dieter Rams' Principles of Good Design
Less, But Better - Dieter Rams' Principles of Good DesignLess, But Better - Dieter Rams' Principles of Good Design
Less, But Better - Dieter Rams' Principles of Good Design
 
Prototyping Your Way to Better and Faster Outcomes
Prototyping Your Way to Better and Faster Outcomes Prototyping Your Way to Better and Faster Outcomes
Prototyping Your Way to Better and Faster Outcomes
 
Cloud Platforms for Java
Cloud Platforms for JavaCloud Platforms for Java
Cloud Platforms for Java
 
Visualizing Relationships: Journalistic Problems in a Digital Age
Visualizing Relationships: Journalistic Problems in a Digital AgeVisualizing Relationships: Journalistic Problems in a Digital Age
Visualizing Relationships: Journalistic Problems in a Digital Age
 
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
3Pillar Global's Kit Unger and Alok Jain to Explore "How Customers Think" at ...
 
Content is King
Content is KingContent is King
Content is King
 

Recently uploaded

Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 

Recently uploaded (20)

Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 

Practical Functional Programming Presentation by Bogdan Hodorog

  • 1. Practical Functional Programming Practical Functional Programming Bogdan Hodorog 8th November 2014 / Functional Angle
  • 2. Practical Functional Programming Introduction Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 3. Practical Functional Programming Introduction About the speaker linkedin.com says Technical Manager my heart says Software engineer is daily reviewing and writing python code sometimes reads and changes elisp code seldom reads c code presently is trying to add golang to his cv in the far past he used java for several years rudimentary knowledge of ruby, javascript, ML
  • 4. Practical Functional Programming Introduction About the speaker linkedin.com says Technical Manager my heart says Software engineer is daily reviewing and writing python code sometimes reads and changes elisp code seldom reads c code presently is trying to add golang to his cv in the far past he used java for several years rudimentary knowledge of ruby, javascript, ML
  • 5. Practical Functional Programming Introduction What is the goal of this talk? intended for programmers using non-strict functional programming languages use something you learned from this presentation in the next week while writing your usual code
  • 6. Practical Functional Programming First class functions Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 7. Practical Functional Programming First class functions First order functions Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 8. Practical Functional Programming First class functions First order functions Context A function’s components a unique identifier, commonly refered as name an parameters list instruction(s), commonly refered a body return value Question? What kind of arguments can be passed? Answer Any kind of objects except functions
  • 9. 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))
  • 10. Practical Functional Programming First class functions Higher order functions Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 11. Practical Functional Programming First class functions Higher order functions Context Question? What kind of arguments can be passed? Answer any type including functions functions must be first-class citizens
  • 12. 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’]
  • 13. 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> c a p i t a l i z e ( team) [ ’ Laura ’ , ’Bogdan ’ , ’ Ti tu s ’ , ’ Z o l i ’ , ’ Alex ’ ] >>> uppercase ( team) [ ’LAURA ’ , ’BOGDAN’ , ’TITUS ’ , ’ ZOLI ’ , ’ALEX ’ ]
  • 14. 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> process ( team , s t r i n g . c a p i t a l i z e ) [ ’ Laura ’ , ’Bogdan ’ , ’ Ti tu s ’ , ’ Z o l i ’ , ’ Alex ’ ] >>> process ( team , s t r i n g . upper ) [ ’LAURA ’ , ’BOGDAN’ , ’TITUS ’ , ’ ZOLI ’ , ’ALEX ’ ]
  • 15. Practical Functional Programming First class functions Higher order functions Generic examples python def high_ord ( par1 , func ) : val = " { } and { } " . format ( par1 , func ( par1 ) ) return val 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 ) ) ( high_ord " something " ( f u n c t i o n c a p i t a l i z e ) )
  • 16. Practical Functional Programming First class functions Higher order functions Decorators Question What about returning a function from a function? python def forbidden ( ) : val = " I can ’ t change t h i s code " return val def decorate ( func ) : def wrapper ( ) : pr int " Something new I want to add " return func ( ) return wrapper forbidden = decorate ( forbidden ) >>> pr int forbidden ( ) Something new I want to add " I can ’ t change t h i s code "
  • 17. Practical Functional Programming First class functions map()/filter()/reduce() Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 18. 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> process ( team , s t r i n g . c a p i t a l i z e ) [ ’ Laura ’ , ’Bogdan ’ , ’ Ti tu s ’ , ’ Z o l i ’ , ’ Alex ’ ] >>> process ( team , s t r i n g . upper ) [ ’LAURA ’ , ’BOGDAN’ , ’TITUS ’ , ’ ZOLI ’ , ’ALEX ’ ]
  • 19. 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> map( s t r i n g . c a p i t a l i z e , team) [ ’ Laura ’ , ’Bogdan ’ , ’ Ti tu s ’ , ’ Z o l i ’ , ’ Alex ’ ] >>> process ( s t r i n g . upper , team) [ ’LAURA ’ , ’BOGDAN’ , ’TITUS ’ , ’ ZOLI ’ , ’ALEX ’ ]
  • 20. Practical Functional Programming First class functions map()/filter()/reduce() filter() python def g e t _ s t a r t s _wi 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 = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> g e t _ s t a r t s _wi t h ( " aeiou " , team) [ ’ alex ’ ]
  • 21. 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 _wi t h ( e l ) : return e l [ 0 ] in " aeiou " team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> f i l t e r ( s t a r t s _wi t h , team) [ ’ alex ’ ]
  • 22. Practical Functional Programming First class functions map()/filter()/reduce() Alternative: enter List Comprehensions python team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> [ 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 " ] [ ’ alex ’ ]
  • 23. 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 = 0 for name in names : r e s u l t += len (name) return r e s u l t team = [ ’ laura ’ , ’ bogdan ’ , ’ t i t u s ’ , ’ z o l i ’ , ’ alex ’ ] >>> count_chars ( team) 24
  • 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 = [ ’ laura ’ , ’ bogdan ’ , ’ t i t u s ’ , ’ z o l i ’ , ’ alex ’ ] >>> reduce ( accumulator , team , 0) 24
  • 25. Practical Functional Programming First class functions map()/filter()/reduce() Practicality solution for reducing code nesting level
  • 26. 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 _wi t h ( e l ) : return e l [ 0 ] in " aeiou " f i l t e r ( s t a r t s _wi t h , team)
  • 27. Practical Functional Programming First class functions map()/filter()/reduce() Practicality solution for reducing code nesting level useful for oneliners
  • 28. Practical Functional Programming First class functions map()/filter()/reduce() Practicality solution for reducing code nesting level useful for oneliners on the other hand, try to debug them :(
  • 29. Practical Functional Programming First class functions map()/filter()/reduce() Practicality solution for reducing code nesting level useful for oneliners on the other hand, try to debug them :( map/filter apart of having aliases might be disguised in other forms (e.g. comprehensions)
  • 30. Practical Functional Programming First class functions Annonymous functions Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 31. Practical Functional Programming First class functions Annonymous functions lambda the term comes from Lambda calculus often refers to a programming language support for annonymous functions often has slightly different semantic in each language python: anonymous function with a single expression as body elisp: anonymous function without a name
  • 32. Practical Functional Programming First class functions Closures Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 33. Practical Functional Programming First class functions Closures Closures Explained function + environment environment contains local variables, params short of lexical closure as opossed to dynamic scoping function’s variables bound where defined not where called favour laziness, they’re active only when called
  • 34. Practical Functional Programming First class functions Closures Lexical scope, not dynamic! python def outer ( ) : global a pr int " global environment : a ={ } " . format ( a ) def inner ( b ) : pr int " inner environment : a ={ } , b ={ } " . format ( a , b ) return a + b a = 2 pr int ( " outer environment : a ={ } " . format ( a ) ) pr int inner ( a+2) a = 10 pr int " r e s u l t i s { } " . format ( outer ( ) ) valid python, lexical scope >>> global environment : a=10 outer environment : a=2 inner environment : a=2 ,b=4 r e s u l t is 6
  • 35. Practical Functional Programming First class functions Closures Lexical scope, not dynamic! python def outer ( ) : global a pr int " global environment : a ={ } " . format ( a ) def inner ( b ) : pr int " inner environment : a ={ } , b ={ } " . format ( a , b ) return a + b a = 2 pr int ( " outer environment : a ={ } " . format ( a ) ) pr int inner ( a+2) a = 10 pr int " r e s u l t i s { } " . format ( outer ( ) ) imaginary dynamic scope >>> global environment : a=10 outer environment : a=2 inner environment : a=10 ,b=4 r e s u l t is 14
  • 36. Practical Functional Programming First class functions Closures Practicality use them for laziness use them for memoization use them to have more expressive code think of them as alternative to classes know your language most languages use just lexical scoping some languages allows switching between lexical and dynamic scoping closures for functions/lambdas/others may behave different
  • 37. Practical Functional Programming First class functions Partial application Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 38. 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 _wi t h ( e l ) : return e l [ 0 ] in " aeiou " team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> f i l t e r ( s t a r t s _wi t h , team) [ ’ alex ’ ]
  • 39. 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 _wi t h ( el , p r e f i x ) : return e l [ 0 ] in p r e f i x team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> f i l t e r ( s t a r t s _wi t h , team) TypeError : s t a r t s _wi t h ( ) takes exac t l y 2 arguments (1 given )
  • 40. Practical Functional Programming First class functions Partial application Solution python def s t a r t s _wi t h ( el , p r e f i x ) : return e l [ 0 ] in p r e f i x import f u n c t o o l s f rozen_func = f u n c t o o l s . p a r t i a l ( s t a r t s _wi t h , p r e f i x =" aeiou " ) team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> f i l t e r ( f rozen_func , team) [ ’ alex ’ ] >>> f rozen_func . func < f u n c t i o n s t a r t s _wi t h at 0x103b4c500> >>> f rozen_func . keywords { ’ p r e f i x ’ : ’ aeiou ’ }
  • 41. Practical Functional Programming First class functions Partial application Solution python def s t a r t s _wi t h ( el , p r e f i x ) : return e l [ 0 ] in p r e f i x import f u n c t o o l s f rozen_func = f u n c t o o l s . p a r t i a l ( s t a r t s _wi t h , p r e f i x =" aeiou " ) team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> f i l t e r ( f rozen_func , team) [ ’ alex ’ ] >>> f rozen_func . func < f u n c t i o n s t a r t s _wi t h at 0x103b4c500> >>> f rozen_func . keywords { ’ p r e f i x ’ : ’ aeiou ’ } Explained curried("aeiou") is a function object waiting to be called its closure contains variable prefix="aeiou"
  • 42. Practical Functional Programming First class functions Partial application Practicality work with map/filter functions enforce some client code based on higher order functions to use fixed value for some arguments often can be replace with lambda python team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> f i l t e r ( lambda e l : s t a r t s _wi t h ( el , " aeiou " ) , team) [ ’ alex ’ ]
  • 43. Practical Functional Programming First class functions Currying Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 44. Practical Functional Programming First class functions Currying Origin named after Haskell Curry (mathematician), not the dish! Lambda calculus theory: says each function has a single param How are multi-params function emulated using this paradigm? Using function composition
  • 45. Practical Functional Programming First class functions Currying Origin named after Haskell Curry (mathematician), not the dish! Lambda calculus theory: says each function has a single param How are multi-params function emulated using this paradigm? Using function composition
  • 46. Practical Functional Programming First class functions Currying Origin named after Haskell Curry (mathematician), not the dish! Lambda calculus theory: says each function has a single param How are multi-params function emulated using this paradigm? Using function composition
  • 47. Practical Functional Programming First class functions Currying Origin named after Haskell Curry (mathematician), not the dish! Lambda calculus theory: says each function has a single param How are multi-params function emulated using this paradigm? Using function composition
  • 48. Practical Functional Programming First class functions Currying Origin named after Haskell Curry (mathematician), not the dish! Lambda calculus theory: says each function has a single param How are multi-params function emulated using this paradigm? Using function composition
  • 49. 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 _wi t h ( el , p r e f i x ) : return e l [ 0 ] in p r e f i x team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> f i l t e r ( s t a r t s _wi t h , team) TypeError : s t a r t s _wi t h ( ) takes exac t l y 2 arguments (1 given )
  • 50. Practical Functional Programming First class functions Currying Solution python def c u r r i e d ( p r e f i x ) : def el_c losure ( e l ) : return e l [ 0 ] in p r e f i x return el_c losure team = [ " laura " , " bogdan " , " t i t u s " , " z o l i " , " alex " ] >>> f i l t e r ( c u r r i e d ( " aeiou " ) , team) [ ’ alex ’ ]
  • 51. Practical Functional Programming First class functions Currying Solution python def mu 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_f return b_f mu 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
  • 52. Practical Functional Programming First class functions Currying Practicality not used in daily situations came accross it in some technical docs
  • 53. Practical Functional Programming First class functions Currying vs Partial application both rely on closure both solve the same problems subtle difference currying returns a function object with single param partial application may return a function object with multiple params
  • 54. Practical Functional Programming Immutability Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 55. 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’]
  • 56. 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 ???
  • 57. 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’]
  • 58. 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]
  • 59. Practical Functional Programming Immutability Practicality mutablity may lead to race conditions immutable objects are considered thread-safe unwanted modifications can be performed on sensitive collections pure functions doesn’t leak side effects some languages support explicit syntax for mutable operations ruby’s bang .methods! scala’s var and val
  • 60. Practical Functional Programming Immutability Practicality
  • 61. Practical Functional Programming Evaluation strategies Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 62. Practical Functional Programming Evaluation strategies Functional languages Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 63. Practical Functional Programming Evaluation strategies Functional languages Eager vs Lazy many of non-strict functional languages use eager(strict) evaluation pass-by-value pass-by-reference pass-by-sharing most strict functional languages use lazy evaluation
  • 64. Practical Functional Programming Evaluation strategies Non-functional languages Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 65. Practical Functional Programming Evaluation strategies Non-functional languages Thunk Reason python def crazy_complex ( i ) : pr int This i s O( nnn ) return i 1 def add_some ( x , some ) : i f some 0: return x + 3 return some pr int add_some ( crazy_complex ( 8 ) , 1) This is O( nnn ) 11
  • 66. Practical Functional Programming Evaluation strategies Non-functional languages Thunk Call by name Evaluate the argument only if needed python def crazy_complex ( i ) : pr int This i s O( nnn ) return i 1 def add_some ( thunk , some ) : i f some 0: return thunk ( ) + 3 return some from f u n c t o o l s import p a r t i a l # or use cur r y i ng instead lazy_thunk = p a r t i a l ( crazy_complex , 8) pr int add_some ( lazy_thunk , 1) This is O( nnn ) 11 pr int add_some ( lazy_thunk , 1) 1
  • 67. Practical Functional Programming Evaluation strategies Non-functional languages Generators Description is a function object behaving like an iterator generates one element at a time python import random team = [ laura , bogdan , t i t u s , z o l i , alex ] 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)
  • 68. Practical Functional Programming Evaluation strategies Non-functional languages Generators Description is a function object behaving like an iterator generates one element at a time python gener . next ( ) ’ bogdan ’ gener . next ( ) ’ laura ’ gener . next ( ) ’ z o l i ’ python gener . next ( ) ’ t i t u s ’ gener . next ( ) ’ z o l i ’ gener . next ( ) ’ laura ’
  • 69. Practical Functional Programming Evaluation strategies Non-functional languages Practicality use thunks to decorate functions thunks can help with lazy evaluation generators offers one element at a time, useful for dealing with streams use generators for inifinte source of data
  • 70. Practical Functional Programming Uncommon topics Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 71. Practical Functional Programming Uncommon topics to look into Tail call optimization: famous topic, but rarely encountered in practice (Scheme, ML, Erlang) Pattern matching
  • 72. Practical Functional Programming Remember checklist Outline 1 Introduction 2 First class functions First order functions Higher order functions map()/filter()/reduce() Annonymous functions Closures Partial application Currying 3 Immutability 4 Evaluation strategies Functional languages Non-functional languages 5 Uncommon topics 6 Remember checklist
  • 73. Practical Functional Programming Remember checklist Asses functional capabilities of a programming language 1 check for first class functions: pass, bound and return 2 look for map/filter/reduce/comprehension and beware of when to use them 3 support for closure 4 annonymous functions and their limit 5 identify immutable types and other related features 6 understand the evaluation strategies employed by the language
  • 74. Practical Functional Programming Remember checklist