0% found this document useful (0 votes)
27 views

SD2201-ch7-Higher Order Functions

The document discusses higher order functions in Python. It describes that a higher order function is a function that takes one or more functions as arguments or returns a function. It provides the map function as an example of a higher order function, explaining that map applies a function to every element in an iterable and returns a new iterable.
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)
27 views

SD2201-ch7-Higher Order Functions

The document discusses higher order functions in Python. It describes that a higher order function is a function that takes one or more functions as arguments or returns a function. It provides the map function as an example of a higher order function, explaining that map applies a function to every element in an iterable and returns a new iterable.
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/ 27

Higher Order Function

[SD2201] Functional Programming


Riksa Meidy Karim S.Kom., M.Si., M.Sc.
Amalya Citra Pradana S.Kom., M.Si., M.Sc.
Outline

01 02

Higher Order Function Map

Description, lib and examples Example and Use Cases


01
Higher Order Function
What is Higher Order Function ?

A function That:

1. Is a First Class
2. Takes a function as an Argument
3. And / Or Return a function as Results
Remember the term First Class?

A first-class function is one that can go anywhere that any other value can go.

There are few to no restrictions.

It means function is treated just like any value.


First-Class ?

1. A number can be stored in a variable and so can a function:


var fortytwo = function() { return 42 };

2. A number can be stored in an array slot and so can a function:


var fortytwos = [42, function() { return 42 }];
First-Class ?

3. A number can be stored in an object field and so can a function:


var fortytwos = {number: 42, fun: function() { return 42 }};

4. A number can be created as needed and so can a function:


42 + (function() { return 42 })();
First-Class ?

5. A number can be passed to a function and so can a function:


function weirdAdd(n, f) { return n + f() }
weirdAdd(42, function() { return 42 });

6. A number can be returned from a function and so can a function:


return 42;
return function() { return 42 };
Let’s try to code it in
Python !
What Does the output says?
What Does it mean?

Function is First Class in Python

We can treat it the same as any other value

Thus, We can create Higher Order Function (HOF)


Creating Our Own HOF

We try to create a HOF that


Accepts an input a , where a is a number
Then return a list as a results of processing a

The processing method of a is in function f


And we take f as another argument for the HOF
Let’s Try This
Well Known HOF

We can also use already existing HOF that is commonly used


Examples:
The map() , filter() and reduce() function
There is also python lib for HOF:
> Functools
> Itertools
02
Map
What is Map ?

1. Input of map is an iterable


2. Also returns iterable
3. Accepts a function input to manipulate the element of the iterable by
changing it ( That’s why it’s called map )
Illustration

X : X * 10

1 10

2 20 Mapping List L using map


function
3 30

4 40

5 50

L L’
How to Use ?

map( function , iterables )


Examples
L = [1,2,3,4,5]
def f(x):
return x * 10
map( f, L)
Remember Lambdas?

We can also use lambdas

L = [1,2,3,4,5]

map( lambda x:x*10, L )


Map with built in python function

We can also use map with built in python function like abs, len ,int , str

Try this:
map( abs , [-1,2,3,5,-6] )
map( len , [ range(2) , range(3) , range(4) ] )
Map with multiple iterables

Using map we can also work with multiple iterables

Map ( function , iterable1 , iterable2..)


Example

L1 = [1,2,3,4,5]

L2 = [ 2,2,3,3,2]

map ( lambda x,y : x**y , L1, L2)

Map ( pow , L1 , L2 )
Consideration

The argument of f function in map(f,iter) corresponds to the number of


iterables.

For example we have pow() function which has 2 arguments the base and the
exponent.

It means if we have k number of arguments in f , we must have k number of


iterables.
Let’s try to code it in
Python !
Latihan

1. Given List P = [‘a’, ‘k’,’u’, ‘l’ , ‘u’ , ‘p’ , ‘a’ ]

We want to make list of tuples of P like this

P’ = [ (1, ’a’ ) , (3, ‘k’), (5,’u’), (7,’l’ ) , (9,’u’), (11,’p’) , (13,’a’) ]


Latihan

2. Terdapat bilangan B

B = 24

Petakan B menjadi list faktor nya!

B’ = [ 1,2,3,4,6,8,12,24]
Latihan

3. Diketahui matriks A,B,C sebagai berikut

A = [ [3 , 4] , [ 5, 6 ] ]

B = [ [1,2] , [ 7 , 8] ]

C = AB

Buatlah program untuk menghitung determinan matriks C menggunakan HOF map!

You might also like