For those who have been programing with Structural or Object Oriented languages functions like Map, Filter, and Reduce might be new to you. They were to me until I started learning more about Functional programming in a Python course that talked about them.
I will explain how they work using the Python’s syntax.
map() helps you to run a function on an
iterable, a loop-like operator.
For sake of simplicity, I’ll use as an example a function that multiplies a given number by 5. Imagine that you want to apply that function to a list of numbers. Commonly, a loop could accomplish it like this,
def multiplyByFive(num): return num*5
We start with,
originalList = [1,2,3,4,5] multipliedList =  for l in originalList: result = multiplyByFive(l) multipliedList.append(result) print(multipliedList) # prints [5, 10, 15, 20, 25]
map() function you just pass two arguments, the
iterable and the function you want to use. The function returns a map. If you cast that map to a list, you get the same result as above.
The map function looks like this,
def multiplyByFive(num): return num*5 originalList = [1,2,3,4,5]
It executes the function N times depending on the size of the iterable. It then returns a map and casts the map into a list.
multipliedList = list(map(multiplyByFive, originalList)) print(multipliedList) # prints [5, 10, 15, 20, 25]
As you can see the code above is much simpler. Even cooler, is that you can pass a labmda function to map so you can reduce the code even more. It would then look like this,
originalList = [1,2,3,4,5]
As before, it executes the function N times depending on the size of the iterable. It then returns a map and casts it into a list
multipliedList = list(map(lambda num: num*5, originalList)) print(multipliedList) # prints [5, 10, 15, 20, 25]
The filter() function filters a given iterable depending on a boolean result. Imagine that you want a list with only even numbers. You could use the filter() function to get that result in an easy way,
originalList = [1,2,3,4,5] multipliedList = list(filter(lambda num: num%2==0, originalList)) print(multipliedList) # prints [2, 4]
As you can see in the code above, we are passing two parameters to the filter() function. Just as with map(), the difference is that the result is either true or false. The filter function returns only the elements that passed the condition.
The reduce() function, also, receives two parameters. The function that is being passed as a parameter gets executed in a rolling way. It executes the function, called labmda expression, with the first two elements on the list, then a value is returned. It gets called again using the returned value and the next value on the list, and so on.
For instance, imagine that you have a list of numbers and you want to get their sum. Instead of making a for loop we could use the reduce() function, like this,
originalList = [1,2,3,4,5] result = reduce(lambda num1, num2: num1+num2 , originalList) print(result) # prints 15
Altogether Now &hellpi;
Another cool thing about these functions is that you could combine them as you wish. Imagine that you want to get the sum of even numbers in a list are after being multiplied by 3. In order to get this result, you can combine all three functions,
originalList = [1,2,3,4,5,6] sum = reduce(lambda num1, num2: num1+num2, list(filter(lambda num: num%2==0, list(map(lambda num: num*3, originalList))))) print(sumatory) # prints 36
- First the
map()function multiplies all the numbers on the list by 3 and converts them to a list.
- Then filter() returns a map with just with the even numbers and we end up with the [6, 12, 18].
- Finally, the reduce() function sums all the elements in the list and returns 36.
As you can see,
reduce() can help shorten your code a lot. They also make it simpler. Statements like for, if, and return are unneeded.
They help as well to work safe with concurrency.
For more information about
reduce() you could check out the following links,
- Benefits of abstracting out control
- Map, Filter and Reduce
If you have further questions, you can contact me as [email protected].