Functional programming is a programming paradigm in which most of the work in a program is done using pure functions. A pure function is a function without any side effects; its return value is always determined by its input arguments, so it always returns the same output, given the same input. In Python, functional programming is supported by the higher-order functions map, filter, and reduce.
map(func, iterable)
filter(func, iterable)
reduce(func, iterable)
map and filter are built-in functions while reduce is present in functools module. These functions can replace for loops and if statements and hence can make the code shorter. So, using these functions makes the code concise as the control flow statements can be replaced by expressions, and the programmer can focus on solving the actual problem instead of going into the details of looping and branching.
These functions are not used much because now Python has better alternatives in the form of comprehensions and generators which we have already seen before. If you are writing your code and you can do the same thing with a comprehension, then it is better to use a comprehension since they are considered more Pythonic and are preferred by the Python community. Although these functions are not used very often, you might encounter them in some code that you are using so it is good to be familiar with them. All these functions have a parameter that accepts a function as an argument. Generally, the functions that are to be sent as arguments to these functions are single-expression throw-away functions. So, we mostly use lambda expressions as arguments to these functions instead of defining separate def statements.
map
map(func, iterable)
The map function takes in a function and an iterable as argument. The function that is sent, should be such that it takes in a single argument and returns a single value. The second argument can be any iterable like list or a tuple; the values inside the iterable should be acceptable as arguments to the first function.
This map function returns a map object which is an iterator in which each item is obtained by applying the argument function to each element of the iterable. An iterator is returned instead of a list for efficiency reasons.
map with multiple iterables
map(func, iterable1, iterable2, ………… )
It is possible to send more than one iterable in the map function, but the condition is that the number of arguments in the function func should be equal to the number of iterables that are sent to the map function. For example, if we send three iterables, then the function func should be such that it takes 3 arguments and returns a single value. The arguments to the function are received from the corresponding iterables. Here is an example-
>>> def multiply(x, y, z):
... return x * y * z
...
>>> map(multiply, [1,2,3,4], (4,5,6,7), [10,20,30,40])
<map object at 0x000002A61232AE30>
filter
The built-in function filter is used to filter out elements of an iterable depending on the result of another function.
filter(func, iterable)
This function takes two arguments, a function and an iterable. The argument function func should be such that it accepts a single argument and returns a Boolean value, which means that it should return either True or False. The second argument can be any iterable like list, set or tuple.
The filter function returns a filter object which is an iterator that produces only those items of iterable for which the function func returns True. The argument function is applied to each element of the iterable; if the function returns True for an element then that element will be produced by the iterator. So, we can say that it filters out all the elements for which the function returns False. Let us understand this with the help of an example.
Reducing an iterable
The function reduce reduces an iterable to a single value, which means that it returns a single value for the whole iterable.
reduce(func, iterable)
This function takes two arguments, first is a function and second is an iterable. The argument function should be such that it takes two arguments and returns a single value. The function reduce works by continually calling the argument function for the successive elements of the iterable, computing and accumulating the results, till the iterable is reduced to a single value.
The argument function is invoked with the first and second values of the iterable, followed by computation of the result. Subsequently, the function is invoked with this result and the third value, with the process repeating for the fourth value and beyond. This continues till all the values in the iterable are used.