Higher Order Functions Shubham Gaikwad



Download 1.86 Mb.
Date02.01.2022
Size1.86 Mb.
#58007
Higher Order Functions (1)

Higher Order Functions

Shubham Gaikwad

2101935

What is higher order function?

Python treats functions as first-class citizens. It simply means that functions are just like other Python objects (e.g., integers, strings, lists, and custom class instances). Means functions can be passed to other functions as arguments, returned from a function and assigned to a variable.

A function is called Higher Order Function if it contains other functions as a parameter or returns a function as an output i.e, the functions that operate with another function are known as Higher order Functions.

Properties of Higher Order Functions

  • A function is an instance of the Object type.
  • You can store the function in a variable.
  • You can pass the function as a parameter to another function.
  • You can return the function from a function.
  • You can store them in data structures such as hash tables, lists, …

def hello(func):

text = func("Hello")

print(text)

hello(loud) #”HELLO”

hello(quiet) #”hello”


def loud(text):

return text.upper()



def quiet(text):

return text.lower()


def divisor(x):

def divisor(x):

def dividend(y):

return y / x

return dividend

divide_by_2 = divisor(2)

print(divide_by_2(10)) #5.0


divide_by_5 = divisor(5)

print(divide_by_5(10)) #2.0


map( ) function

map() function returns a map object(which is an iterator) of the results after applying the given function to each item of a given iterable (list, tuple etc.)

map(mapping_function, iterable)

mapping_function : It is a function to which map passes each element of given iterable.

iterable : It is a iterable which is to be mapped.

numbers = [1,2,3,4]

numbers = [1,2,3,4]


def square(n):

return n*n

squares = list(map(square, numbers))

print(squares) #[1,4,9,16]

squares = list(map(lambda n: n*n, numbers))

print(squares) #[1,4,9,16]


filter( ) function

The filter() function filters the given sequence with the help of a function that tests each element in the sequence to be true or not.

filter(function, iterable)

function : It is a boolean function that tests if each element of iterable passes a certain condition

iterable : It is a sequence which needs to be filtered.

numbers = [-1,2,-3,4]

numbers = [-1,2,-3,4]

positives = filter(lambda n: n > 0, numbers)

print(list(positives)) # [2,4]

reduce( ) function

Unlike the map() and filter() functions which are built-in functions, the reduce() function is available in the functools module. The reduce( ) function will apply the function to the elements of the iterable using the rolling result cumulatively before it arrives at a final output value.

reduce(function, iterable)


Guido Van Rossum

Python Creator

“ Use functools.reduce( ) if you

really need it; however,

99% of the time an explicit



for loop is more readable.”

Data: [a1, a2, a3, …, an]

Data: [a1, a2, a3, …, an]

Function: f(x, y)

reduce(f, data):

Step 1: val1 = f(a1, a2)

Step 2: val2 = f(val1, a3)

Step 3: val3 = f(val2, a4)

Step n-1: valn-1 = f(valn-2, an)

Returns valn-1

from functools import reduce

from functools import reduce

numbers = [1,2,3,4]

total = reduce(lambda a, b: a + b, numbers)

print(total) #10


T

Thank You!
Download 1.86 Mb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page