Map, Filter and Reduce
These are three functions which facilitate a functional approach to programming.
Map
Applies a function to all the items in an input_list.
Example: Pass all the list elements to a function one-by-one and then collect the output.
Normal way:
With Map
:
Most of the times we use lambdas with map
. Instead of a list of inputs we can even have a list of functions!
Filter
Create a list of elements for which a function returns true.
Example:
The filter resembles a for loop but it is a builtin function and faster.
Reduce
Perform some computation on a list and return the result. It applies a rolling computation to sequential pairs of values in a list.
Example, compute the product of a list of integers.
The normal way:
Now with reduce:
Last updated