Higher Order Functions in Python.
Learn the fundamentals of higher order functions in python programming language.
Hello readers!
In this article we will discuss about what is higher order functions, how to use them and how to implement them in our code. First let us discuss about higher order function. Higher order function is nothing but a function which takes function as a argument or returns a function. This type of functions is generally implemented in many languages such as Python, JavaScript, Go etc.
It became a trend that this concept is asked by the interviewers during the interviews. I have made a conversation with many developers and asking about this concept, but they are unaware of this word, rather they are using this concept daily without their knowledge. So I decided to make an article about this and definitely this will helpful for them.
Examples
Let’s get into the topic and starts with an example, so that you will get clear idea about higher order function. Now we will get into the advance functions for building popular functions in our forthcoming examples.
Function as a argument
Before starting this, let’s create a simple function called doOperation that takes three arguments:
- Function operation
- number1
- number2
And then we will create an another operation called sumBothNumbers that simply returns the difference of two numbers.
Let’s see an example:
def doOperation(operation, number1, number2):
return operation(number1, number2)
def sumBothNumbers(number1, number2):
return number1 + number2
doOperation(sumBothNumbers, 4, 5)
------------
Output
------------
9From the above example, the case that contains doOperation function is a redundant one. The doOperation is a part of library where we can able to extend with our own operations.
Returning a function
Next we are going to build a higher order function used to return a function. In our function we called it as multiplyBy and it will take the number as argument and returns a function that will multiply the input with that number.
def multiplyBy(multiplier):
def result(num):
return num * multiplier
return result
multiplyByThree = multiplyBy(3)
multiplyByThree(4)
------------
Output
------------
12Building filter(), map() and reduce()
Now let’s build a simple functions using higher order function.
filtering()
The filtering() function contains two parameters, an array and test function.
It will return a new array with all elements that passes the test.
def filtering(arr, test):
passed = []
for element in arr:
if (test(element)):
passed.append(element)
return passed
def isSuperNumber(num):
return num >= 10
filtering([1, 5, 11, 3, 22], isSuperNumber)
------------
Output
------------
[11, 22]From the above example, it clear shows that filter() function is easy to code and use. In this example we will get all the super numbers from an array.
mapping()
The mapping() function contains two parameters, an array and a transform function.
It will return a new transformed array in which each item in an array is the result of transform function called over each element of the original array.
def mapping(arr, transform):
mapped = []
for element in arr:
mapped.append(transform(element))
return mapped
def addTwo(num):
return num+2
mapping([1, 2, 3], addTwo)
------------
Output
------------
[3, 4, 5]reducing()
The reducing() function contains three parameters, a reducer function, an initial value for accumulator, and an array.
For each item in the array, the reducer function is called and passing it to the accumulator, current array element.
Then the return value is assigned to the accumulator. When reducing all the elements in the list gets finished, the accumulator value is returned.
def reducing(reducer, initial, arr):
acc = initial
for element in arr:
acc = reducer(acc, element)
return acc
def accum(acc, curr):
return acc + curr
reducing(accum, 0, [1, 2, 3])
------------
Output
------------
6Conclusion
I hope you enjoyed a lot and learnt something new today. Next time when you get to the interview you will know about the function, where it will returned as a parameter. Then you will know that you are dealing with higher order functions.
Thanks for reading!






