Map, Filter, Reduce

Map, Filter, Reduce

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()

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,

We start with,

With the 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,

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.

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,

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

filter()
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,

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.

reduce()

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,

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,

  • 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.

Finally

As you can see, map(), filter() and 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 map(), filter() and reduce() you could check out the following links,

If you have further questions, you can contact me as [email protected].

Focus Mode

Contact Request

Close

We will call you right away. All information is kept private