Python: High Order Functions
Lamda Function
• Function without name (anonymous)
• Lambda functions can only be comprised of
a single expression
– No loops, no calling other methods
– Lambda functions can take any number of
variables
• Syntax:
lambda param1,…,paramn : expression
Exmples
False
56
Higher Order Function
• A higher-order function is a function that takes
another function as a parameter
• They are “higher-order” because it’s a function of a
function
• Examples
– Map
– Reduce
– Filter
• Lambda can work as a parameter to higher-order
functions
Higher Order Function
source: [Link]
Higher Order Function
source: [Link]
Higher Order Function
source: [Link]
map function
map(function, iterable, ...)
• Map applies function to each element of
iterable (sequence/list) and creates a list of the
results
map function
source: [Link]
map function
source: [Link]
map function
[Link]
map function
source: [Link]
map function
source: [Link]
reduce function
Reduce takes a binary function and a list, and
returns a single value, which is obtained by
repeatedly applying the binary function to pairs
of elements in the list.
So, if the list contains elements x1,x2,…xn and
the function is f then the result will be
f(...f(f(x1,x2),x3),...xn)
reduce function
source: [Link]
reduce function
source: [Link]
reduce function
source: [Link]
reduce function
source: [Link]
filter function
source: [Link]
filter function
source: [Link]