[go: up one dir, main page]

0% found this document useful (0 votes)
38 views45 pages

Case Study: Python: Functional Programming

The document discusses functional programming in Python. It covers immutable data, lambda functions, first-class functions, and high-order functions like map, filter and reduce. Lambda functions allow defining anonymous functions inline. First-class functions means functions can be assigned to variables or passed as arguments. Map applies a function to each element of a sequence. Filter returns elements where the function evaluates to True.

Uploaded by

Tân Như Đinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views45 pages

Case Study: Python: Functional Programming

The document discusses functional programming in Python. It covers immutable data, lambda functions, first-class functions, and high-order functions like map, filter and reduce. Lambda functions allow defining anonymous functions inline. First-class functions means functions can be assigned to variables or passed as arguments. Map applies a function to each element of a sequence. Filter returns elements where the function evaluates to True.

Uploaded by

Tân Như Đinh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Case Study: Python

Functional Programming

Dr. Nguyen Hua Phung

HCMC University of Technology, Viet Nam

08, 2020

Dr. Nguyen Hua Phung Case Study: Python 1/1


Functional Programming

Immutable Data
lambda function
First-class functions
High-order functions: map, filter, reduce
Closure
Decorator

Dr. Nguyen Hua Phung Case Study: Python 2/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b
(lambda a,b: a + b)(3,4)

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b
(lambda a,b: a + b)(3,4) => 7

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b
(lambda a,b: a + b)(3,4) => 7
x = lambda a,b: a + b

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b
(lambda a,b: a + b)(3,4) => 7
x = lambda a,b: a + b
x(3,4)

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b
(lambda a,b: a + b)(3,4) => 7
x = lambda a,b: a + b
x(3,4) => 7

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b
(lambda a,b: a + b)(3,4) => 7
x = lambda a,b: a + b
x(3,4) => 7
Anonymous function

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b
(lambda a,b: a + b)(3,4) => 7
x = lambda a,b: a + b
x(3,4) => 7
Anonymous function
Any number of parameters

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b
(lambda a,b: a + b)(3,4) => 7
x = lambda a,b: a + b
x(3,4) => 7
Anonymous function
Any number of parameters
Body is just one expression

Dr. Nguyen Hua Phung Case Study: Python 3/1


Lambda function

Syntax:
lambda ( <param > ( , <param ) ∗ ) ? : <exp>
For example,
lambda a,b: a + b
(lambda a,b: a + b)(3,4) => 7
x = lambda a,b: a + b
x(3,4) => 7
Anonymous function
Any number of parameters
Body is just one expression
Used in high-order functions

Dr. Nguyen Hua Phung Case Study: Python 3/1


First-class function
A function is treated as any other value, i.e. it is
assigned to a variable
def f o o ( a , b ) : pass
x = foo
x (3 ,4)

Dr. Nguyen Hua Phung Case Study: Python 4/1


First-class function
A function is treated as any other value, i.e. it is
assigned to a variable
def f o o ( a , b ) : pass
x = foo
x (3 ,4)
passed into another function as a parameter
def f o o ( f , x ) :
return f ( x )
f o o ( lambda a : a ∗∗ 2 , 4 )

Dr. Nguyen Hua Phung Case Study: Python 4/1


First-class function
A function is treated as any other value, i.e. it is
assigned to a variable
def f o o ( a , b ) : pass
x = foo
x (3 ,4)
passed into another function as a parameter
def f o o ( f , x ) :
return f ( x )
f o o ( lambda a : a ∗∗ 2 , 4 ) => 16

Dr. Nguyen Hua Phung Case Study: Python 4/1


First-class function
A function is treated as any other value, i.e. it is
assigned to a variable
def f o o ( a , b ) : pass
x = foo
x (3 ,4)
passed into another function as a parameter
def f o o ( f , x ) :
return f ( x )
f o o ( lambda a : a ∗∗ 2 , 4 ) => 16
returned as a value
def f ( x ) :
def g ( y ) :
return x ∗ y
return g
m = f (3)
m( 4 )
Dr. Nguyen Hua Phung Case Study: Python 4/1
First-class function
A function is treated as any other value, i.e. it is
assigned to a variable
def f o o ( a , b ) : pass
x = foo
x (3 ,4)
passed into another function as a parameter
def f o o ( f , x ) :
return f ( x )
f o o ( lambda a : a ∗∗ 2 , 4 ) => 16
returned as a value
def f ( x ) :
def g ( y ) :
return x ∗ y
return g
m = f (3)
m( 4 ) => 12
Dr. Nguyen Hua Phung Case Study: Python 4/1
High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
=> [97.7, 98.6, 99.5, 100.4, 102.2]

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
list(map(lambda x,y: x + y,[1,2,3],[4,5,6,7]))

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
list(map(lambda x,y: x + y,[1,2,3],[4,5,6,7])) => [5,7,9]

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
list(map(lambda x,y: x + y,[1,2,3],[4,5,6,7]))
filter(<function>,<sequence>) return an iterator that
contains elements in <sequence> for which
<function> returns True

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
list(map(lambda x,y: x + y,[1,2,3],[4,5,6,7]))
filter(<function>,<sequence>) return an iterator that
contains elements in <sequence> for which
<function> returns True
list(map(lambda c: c % 2 == 1, [0,1,2,3,4,5]))

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
list(map(lambda x,y: x + y,[1,2,3],[4,5,6,7]))
filter(<function>,<sequence>) return an iterator that
contains elements in <sequence> for which
<function> returns True
list(map(lambda c: c % 2 == 1, [0,1,2,3,4,5])) => [1,3,5]

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
list(map(lambda x,y: x + y,[1,2,3],[4,5,6,7]))
filter(<function>,<sequence>) return an iterator that
contains elements in <sequence> for which
<function> returns True
list(map(lambda c: c % 2 == 1, [0,1,2,3,4,5]))
reduce(<function>,<sequence>(,<initial>)?): if
<sequence> is [s1 ,s2 ,s3 ], reduce return
function(function(s1 ,s2 ),s3 ) or
function(function(function(<initial>,s1 ),s2 ),s3 )

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
list(map(lambda x,y: x + y,[1,2,3],[4,5,6,7]))
filter(<function>,<sequence>) return an iterator that
contains elements in <sequence> for which
<function> returns True
list(map(lambda c: c % 2 == 1, [0,1,2,3,4,5]))
reduce(<function>,<sequence>(,<initial>)?): if
<sequence> is [s1 ,s2 ,s3 ], reduce return
function(function(s1 ,s2 ),s3 ) or
function(function(function(<initial>,s1 ),s2 ),s3 )
from functools import reduce

Dr. Nguyen Hua Phung Case Study: Python 5/1


High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
list(map(lambda x,y: x + y,[1,2,3],[4,5,6,7]))
filter(<function>,<sequence>) return an iterator that
contains elements in <sequence> for which
<function> returns True
list(map(lambda c: c % 2 == 1, [0,1,2,3,4,5]))
reduce(<function>,<sequence>(,<initial>)?): if
<sequence> is [s1 ,s2 ,s3 ], reduce return
function(function(s1 ,s2 ),s3 ) or
function(function(function(<initial>,s1 ),s2 ),s3 )
from functools import reduce
reduce(lambda x,y: x+y,[1,2,3,4])
Dr. Nguyen Hua Phung Case Study: Python 5/1
High-order functions: map, filter, reduce

map(<function>,<sequence>): apply <function> to


each element of <sequence> and return an iterator
cels = [36.5, 37, 37.5, 38, 39]
fahr = list(map(lambda c: (float(9) / 5) * c + 32,cels))
list(map(lambda x,y: x + y,[1,2,3],[4,5,6,7]))
filter(<function>,<sequence>) return an iterator that
contains elements in <sequence> for which
<function> returns True
list(map(lambda c: c % 2 == 1, [0,1,2,3,4,5]))
reduce(<function>,<sequence>(,<initial>)?): if
<sequence> is [s1 ,s2 ,s3 ], reduce return
function(function(s1 ,s2 ),s3 ) or
function(function(function(<initial>,s1 ),s2 ),s3 )
from functools import reduce
reduce(lambda x,y: x+y,[1,2,3,4]) => 10
Dr. Nguyen Hua Phung Case Study: Python 5/1
Example

class Exp (ABC ) : pass


class I n t L i t ( Exp ) : # v a l : i n t
class BinExp ( Exp ) : #op : s t r , l e f t : Exp , r i g h t : Exp
exp = [ 1 2 , ( " + " , 2 3 ) , ( "−" , 1 4 ) ]
reduce ( lambda acc , e l e :
BinExp ( e l e [ 0 ] , acc , I n t L i t ( e l e [ 1 ] ) ) ,
exp [ 1 : ] , I n t L i t ( exp [ 0 ] ) )

Dr. Nguyen Hua Phung Case Study: Python 6/1


Example

class Exp (ABC ) : pass


class I n t L i t ( Exp ) : # v a l : i n t
class BinExp ( Exp ) : #op : s t r , l e f t : Exp , r i g h t : Exp
exp = [ 1 2 , ( " + " , 2 3 ) , ( "−" , 1 4 ) ]
reduce ( lambda acc , e l e :
BinExp ( e l e [ 0 ] , acc , I n t L i t ( e l e [ 1 ] ) ) ,
exp [ 1 : ] , I n t L i t ( exp [ 0 ] ) )
BinExp

"-" BinExp IntLit(14)

"+" IntLit(12) IntLit(23)

Dr. Nguyen Hua Phung Case Study: Python 6/1


Example

class Exp (ABC ) : pass


class I n t L i t ( Exp ) : # v a l : i n t
class BinExp ( Exp ) : #op : s t r , l e f t : Exp , r i g h t : Exp
exp = [ 1 2 , ( " + " , 2 3 ) , ( "−" , 1 4 ) ]
reduce ( lambda acc , e l e :
BinExp ( e l e [ 0 ] , acc , I n t L i t ( e l e [ 1 ] ) ) ,
exp [ 1 : ] , I n t L i t ( exp [ 0 ] ) )
BinExp
acc = IntLit(exp[0])
for ele in exp[1:]:
"-" BinExp IntLit(14) acc = BinExp(ele[0],
acc,
"+" IntLit(12) IntLit(23) IntLit(exp[1]))

Dr. Nguyen Hua Phung Case Study: Python 6/1


Closure

Closure is a function object together with an


environment.
def power ( y ) :
def i n n e r ( x ) :
r e t u r n x ∗∗ y
return inner
square= power ( 2 )
square ( 5 )

Dr. Nguyen Hua Phung Case Study: Python 7/1


Closure

Closure is a function object together with an


environment.
def power ( y ) :
def i n n e r ( x ) :
r e t u r n x ∗∗ y
return inner
square= power ( 2 )
square ( 5 ) => 25

Dr. Nguyen Hua Phung Case Study: Python 7/1


Decorator

Decorator allows to modify the behavior of function


or class without permanently modifying it.

Dr. Nguyen Hua Phung Case Study: Python 8/1


Decorator

Decorator allows to modify the behavior of function


or class without permanently modifying it.

def f o o ( x , y ) :
r e t u r n x∗y
print ( foo ( 3 , 4 ) )

Dr. Nguyen Hua Phung Case Study: Python 8/1


Decorator

Decorator allows to modify the behavior of function


or class without permanently modifying it.

def f o o ( x , y ) :
r e t u r n x∗y
print ( foo ( 3 , 4 ) ) => 12

Dr. Nguyen Hua Phung Case Study: Python 8/1


Decorator

Decorator allows to modify the behavior of function


or class without permanently modifying it.

@log_decorator
def f o o ( x , y ) :
r e t u r n x∗y
print ( foo ( 3 , 4 ) )

Dr. Nguyen Hua Phung Case Study: Python 8/1


Decorator

Decorator allows to modify the behavior of function


or class without permanently modifying it.

@log_decorator
def f o o ( x , y ) :
r e t u r n x∗y
print ( foo ( 3 , 4 ) )
=> foo is running
=> 12

Dr. Nguyen Hua Phung Case Study: Python 8/1


Decorator

Decorator allows to modify the behavior of function


or class without permanently modifying it.

@log_decorator
def f o o ( x , y ) :
r e t u r n x∗y
print ( foo ( 3 , 4 ) )

How?
def l o g _ d e c o r a t o r ( f u n c ) :
def i n n e r ( ∗ arg ) :
p r i n t ( f u n c . __name__+ " i s r u n n i n g " )
r e t u r n f u n c ( ∗ arg )
return inner

Dr. Nguyen Hua Phung Case Study: Python 8/1


Decorator

Decorator allows to modify the behavior of function


or class without permanently modifying it.

@log_decorator def f o o ( x , y ) :
def f o o ( x , y ) : r e t u r n x∗y
r e t u r n x∗y foo = log_decorator ( foo )
print ( foo ( 3 , 4 ) ) print ( foo ( 3 , 4 ) )

How?
def l o g _ d e c o r a t o r ( f u n c ) :
def i n n e r ( ∗ arg ) :
p r i n t ( f u n c . __name__+ " i s r u n n i n g " )
r e t u r n f u n c ( ∗ arg )
return inner

Dr. Nguyen Hua Phung Case Study: Python 8/1


References I

[1] Python Tutorial, http:w3schools.com/python, 10 08 2020.

[2] Python Programming Language, https://www.


geeksforgeeks.org/python-programming-language/,
10 08 2020.
[3] Python Tutorial,
https://www.tutorialspoint.com/python, 10 08 2020.

[4] Introduction to Python 3,


https://realpython.com/python-introduction/, 10 08
2020.

Dr. Nguyen Hua Phung Case Study: Python 9/1

You might also like