avatarSathishravi

Summary

The website content provides an explanation and examples of higher-order functions in Python, discussing their ability to take functions as arguments or return functions, and demonstrating how to implement common functions like filter(), map(), and reduce().

Abstract

The article on the undefined website delves into the concept of higher-order functions within the Python programming language. It begins by defining higher-order functions as those that can accept other functions as parameters or return a function as a result. The author emphasizes the importance of understanding this concept, noting that while many developers use it daily, they may not be familiar with the term "higher-order functions." The article proceeds to illustrate the concept with examples, such as a function that performs an operation based on a passed function and two numbers, and another that returns a function to multiply numbers by a fixed multiplier. Furthermore, the article demonstrates the implementation of filter(), map(), and reduce() functions, which are classic examples of higher-order functions that process collections of data in functional programming styles. The author concludes by expressing the hope that readers have gained valuable knowledge that could be beneficial in interviews and everyday coding practices.

Opinions

  • The author believes that higher-order functions are a fundamental concept that many developers use without being consciously aware of it.
  • The article suggests that understanding higher-order functions can be advantageous for interviews, implying that this knowledge is commonly assessed in technical interviews.
  • The author conveys that higher-order functions are a powerful tool in Python, as they allow for the creation of more abstract and flexible code.
  • By providing practical examples, the author indicates that hands-on learning with real code snippets is an effective way to grasp programming concepts.
  • The author seems to encourage daily use of higher-order functions for their utility in building concise and maintainable codebases.

Higher Order Functions in Python.

Learn the fundamentals of higher order functions in python programming language.

Photo by Faisal M on Unsplash

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
------------
9

From 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
------------
12

Building 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
------------
6

Conclusion

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!

Programming
Python
Tech
Software Development
Web Development
Recommended from ReadMedium