
Python Functional Programming: Higher-Order Functions Explained
Discover the power of higher-order functions in Python functional programming. Learn how functions can be used as first-class objects, passed as parameters, and even returned from other functions. Explore examples of higher-order functions and unleash the full potential of functional programming in Python.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Python Functional Programming Higher Order Higher Order Functions Functions By Dr. Ziad Al-Sharif
Functional programming Functional programming Python supports functional programming idioms Built-ins for map(), filter(), zip(), reduce(), etc. These are often used with lambda
Functions are first Functions are first- -class objects class objects Functions can be used as any other datatype, e.g.: Arguments to function Return values from a function (remember closure, lambda, etc.) Assigned to variables Parts of tuples, lists, etc. Functions as parameters Have you ever wanted to pass an entire function as a parameter Python has functions as first-class citizens, so you can do this You simply pass the functions by name >>>defsquare(x): return x*x >>>defapplier(q, x): return q(x) >>>applier(square, 7) 49
Higher Higher- -Order Order Functions: Example 1 Functions: Example 1 # function are passed as arguments # to another function defshout(txt): return txt.upper() defwhisper(txt): return txt.lower() defgreet(fun): # storing the function in a variable hi = fun("Hi, I m a Higher-Order Function.") print(hi) greet(shout) greet(whisper)
Higher Higher- -Order Order Functions: Example 2 Functions: Example 2 # A function can return another function def outer_fun(x): def inner_fun(y): return x * y return inner_fun mul_by_ten = outer_fun(10) print(mul_by_ten(1)) print(mul_by_ten(2))
Higher Higher- -Order Functions Order Functions A function is called Higher Order Function contains other functions as a parameter or returns a function as an output e.g. the functions that operate with another function are known as Higher order Functions. Often, a higher higher- -order order is a function of a function Higher Order Function if it: Many useful functions are considered higher map() filter() reduce() zip() Lambda works great as a parameter to higher-order functions if you can deal with its limitations higher- -order order functions
Python map() map() Function
Python map map() () Function A built-in function that allows you to process and transform all the items in an iterable without using an explicit for loop. Useful when you need to apply a transformation function to each item in an iterable and transform them into a new iterable. Takes a function object and an iterable (or multiple iterables) as arguments and returns an iterator that yields transformed items on demand. One of the tools that support a functional programming style in Python. Syntax: map(function, iterable[, iterable1,..., iterableN]) Applies function to each item in iterable in a loop and returns a new iterator that yields transformed items on demand. function can be any Python function that takes a number of arguments equal to the number of iterables you pass to map().
>>>defsquare(x): return x ** 2 >>> L = [1, 2, 3, 4, 5] >>> obj = map(square, L) >>> L2 = list(obj) [1, 4, 9, 16, 25] >>> >>> L = [1, 2, 3, 4, 5] >>> L2 = [] >>>for num in L: L2.append(num ** 2) >>> L2 [1, 4, 9, 16, 25] >>> >>>square lambda x:x**2 >>> L = [1, 2, 3, 4, 5] >>> L2=list(map(square,L)) >>> L2 [1, 4, 9, 16, 25] >>> >>> L = [1, 2, 3, 4, 5] >>> L2=list(map(lambda x:x**2,L)) >>> L2 [1, 4, 9, 16, 25] >>>
map() map() >>> # Convert to floating-point >>>list(map(float,["12.3", "3.3", "-15.2"])) [12.3, 3.3, -15.2] >>> >>> # Convert to integer >>>list(map(int,["12", "3", "-15"])) [12, 3, -15] >>>
map() map() map(function, sequence) calls function(item) for each of the sequence's items and returns a map object s. For example, to compute some cubes: >>> def cube(x): return x*x*x >>> >>> obj = map(cube, range(1, 11)) >>> L = list(obj) >>> L [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] >>> >>> >>> def square(x): return x*x >>> seq = range(8) >>> list(map(lambda x,y:(x,y), seq, map(square, seq))) [(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49)] >>>
map() map() Map applies function object object of the results You can optionally provide more iterables place tuples in the result list Map returns an iterator which can be cast to list function to each element of an iterable and creates a map map iterables as parameters to map and it will map(function, iterable, ...) >>>nums = [1, 2, 3, 4, 5] >>> res = map(lambda x : x * 10, nums) >>>type(res) <class 'map'> >>> res <map object at 0x000001CE48288610> >>> L = list(res) >>> L [10, 20, 30, 40, 50]
map() map() : : Examples Examples Note: + is an operator, not a function We can define a corresponding add function >>>defadd(x, y): return x + y >>>list(map(add,[1,2,3,4],[100,200,300,400])) [101, 202, 303, 404] >>> Or import the operator module >>>from operator import * >>>list(map(add, [1,2,3,4], [100,200,300,400])) [101, 202, 303, 404] >>>list(map(sub, [1,2,3,4], [100,200,300,400])) [-99, -198, -297, -396] >>>
map() map() : Examples : Examples nums = [1, 2, 3, 4, 5] res = map(lambda x : x * 10, nums) print(type(res)) print(res) L = list(res) print(L) <class 'map'> <map object at 0x0000020E15889460> [10, 20, 30, 40, 50] >>> >>> nums = [1, 2, 3, 4, 5] res = map(lambda x : (x * 10, x + 10), nums) print(type(res)) print(res) L = list(res) print(L) <map object at 0x0000027CC1669A60> [(10,11),(20,12),(30,13),(40,14),(50,15)] >>> >>> <class 'map'>
map() map() : : Examples Examples def fun(x,y): return x * y, x + y num1 = [1, 2, 3, 4, 5] num2 = [10, 20, 30, 40, 50] L1 = list(map(lambda x,y : (x * y, x +y), num1, num2)) print(L1) L2 = list(map(fun, num1, num2)) print(L2) >>> [(10,11), (40,22), (90,33), (160,44), (250,55)] [(10,11), (40,22), (90,33), (160,44), (250,55)] >>>
map(): map(): Exercise Exercise Goal: given a list of 3D points (x,y,z) in the form of tuples, create a new list consisting of the distances of each point from the origin (0,0,0) #Loop Based Solution points=[(2, 1, 3), (5, 7, -3), (2, 4, 0), (9, 6, 8)] from math import sqrt def distance(point) : x, y, z = point return sqrt(x**2 + y**2 + z**2) #loop through the list and add results to a new list L = [] for t in points: L.append(distance(t)) print(L) from math import sqrt points = [(2, 1, 3), (5, 7, -3), (2, 4, 0), (9, 6, 8)] def distance(point) : x, y, z = point return sqrt(x**2 + y**2 + z**2) L = list(map(distance, points))
Python filter() filter() Function
Python filter filter() () Function A built-in function that allows you to process an iterable and extract those items that satisfy a given condition. Applies a filtering function to an iterable and produce a new iterable with the items that satisfy the condition at hand. Syntax: filter(function, iterable)
filter() filter(function, iterable) The filter runs through each element of iterable (any iterable object such as a List or another collection) It applies function to each element of iterable If function returns True for that element then the element is put into a List Then, filter returns an iterator which can be cast to type list with list() >>> nums = [0, 4, 7, 2, 1, 0 , 9 , 3, 5, 6, 8, 0, 3] >>> >>> obj = filter(lambda x : x != 0, nums) <filter object at 0x0000024304038250> >>> L = list(obj) >>> L [4, 7, 2, 1, 9, 3, 5, 6, 8, 3] >>>
Example: if you need to process a list of numbers and return a new list containing only those numbers greater than 0 >>> numbers = [-2, -1, 0, 1, 2] >>>def extract_positive(numbers): positives = [] for number in numbers: if number > 0: # Filtering condition positives.append(number) return positives >>> extract_positive(numbers) [1, 2] >>> >>> # Using a user-defined function >>>def is_positive(n): return n > 0 >>>list(filter(is_positive, numbers)) [1, 2] >>>
Python zip() zip() Function
zip zip() Function () Function The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc. Syntax Syntax: zip(iterator1, iterator2, iterator3 ...) >>> a = ("John", "Charles", "Mike") >>> b = ("Jenny", "Christy", "Monica") >>> x = zip(a, b) >>> x <zip object at 0x00000218F30B1500> >>> L = list(x) >>> L [('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica')] >>>
zip zip() Function () Function If the passed iterators have different lengths, then the iterator with the least items decides the length of the new iterator. >>> a = ("John", "Charles", "Mike") >>> b = ("Jenny", "Christy", "Monica", "Vicky") >>> x = zip(a, b) >>> x <zip object at 0x00000218F30B1500> >>> L = list(x) >>> L [('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica')] >>>
Python reduce() reduce() Function
Python reduce reduce() () Function Useful when you need to apply a function to an iterable and reduce it to a single cumulative value. Unlike filter() and map(), which are still built-in functions, reduce() was moved to the functoolsmodule. A call to reduce() starts by applying function to the first two items in iterable. This way, it computes the first cumulative result, called an accumulator. Then reduce() uses the accumulator and the third item in iterable to compute the next cumulative result. The process continues until the function returns with a single value. Syntax: reduce(function, iterable, initial)
>>>from functools importreduce >>> >>> L = [1, 2, 3, 4, 5, 6] >>> >>>def is_even(n): return n % 2 == 0 >>> even_numbers = list(filter(is_even, L)) >>> >>>reduce(lambda a,b: a+b, even_numbers) 12 >>> >>>reduce(lambda a,b: a+b, filter(is_even, L)) 12 >>>
reduce() reduce() reduce(function, iterable[,initializer]) The reduce(fun, seq) function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along. This function is defined in functools module. Reduce will apply function to each element in iterable along with the sum so far and create a cumulative sum of the results function must take two parameters If initializer is provided, initializer will stand as the first argument in the sum However, the reduce() reduce() function requires an import import statement for the functools >>>fromfunctoolsimportreduce >>> nums = [1, 2, 3, 4, 5, 6, 7, 8] >>> obj = reduce(lambda x, y : (x, y), nums) >>> obj (((((((1, 2), 3), 4), 5), 6), 7), 8) >>>
reduce reduce() () : Examples 1 : Examples 1 # importing functools for reduce() import functools # initializing list L = [ 1 , 3, 5, 6, 2 ] # using reduce to compute sum of list print("The sum of L is : ") print(functools.reduce(lambda a,b : a+b, L)) # using reduce to compute maximum element from list print("The maximum element in L is : ") print(functools.reduce(lambda a,b : a if a > b else b, L)) The sum of L is : 17 The maximum element in L is : 6
reduce reduce() () : Examples 2 : Examples 2 # importing functools for reduce() importfunctools # importing operator for operator functions importoperator # initializing list L = [ 1 , 3, 5, 6, 2 ] print ("The sum of the list elements is : ") print (functools.reduce(operator.add, L)) print ("The product of list elements is : ") print (functools.reduce(operator.mul, L)) The sum of the list elements is : 17 The product of list elements is : 180
Summary Summary: : map map, , filter filter, , reduce reduce >>> def add1(x): return x+1 >>> def odd(x): return x%2 != 0 >>> def add(x,y): return x + y >>> map(add1, [1,2,3,4]) [2, 3, 4, 5] >>> map(+,[1,2,3,4],[100,200,300,400]) map(+,[1,2,3,4],[100,200,300,400]) ^ SyntaxError: invalid syntax >>> map(add,[1,2,3,4],[100,200,300,400]) [101, 202, 303, 404] >>> reduce(add, [1,2,3,4]) 10 >>> filter(odd, [1,2,3,4]) [1, 3]
: Python s higher Summary Summary: Python s higher- -order functions order functions Python supports higher-order functions that operate on lists But many Python programmers prefer to use list comprehensions, instead >>>defsquare(x): return x*x >>>defeven(x): return 0 == x % 2 >>>map(square, range(10,20)) [100, 121, 144, 169, 196, 225, 256, 289, 324, 361] >>>filter(even, range(10,20)) [10, 12, 14, 16, 18] >>>map(square, filter(even, range(10,20))) [100, 144, 196, 256, 324]
Summary Summary: : filter filter vs. vs. reduce reduce Python has build-ins for reduce and filter >>>from functools import reduce >>>from operator import add >>>reduce(add, [1,2,3,4]) 10 >>>defodd(x): return 0 != x % 2 >>>list(filter(odd, [1,2,3,4])) [1, 3]
Reference: Reference: map() https://docs.python.org/3/library/functions.html#map filter() https://docs.python.org/3/library/functions.html#filter reduce() https://realpython.com/python-reduce-function/