9.6 Combining the map and reduce transformations
In the other recipes in this chapter, we’ve been looking at map, filter, and reduce operations. We’ve looked at each of these functions in isolation:
The Applying transformations to a collection recipe shows the map() function.
The Picking a subset – three ways to filter recipe shows the filter() function.
The Summarizing a collection – how to reduce recipe shows the reduce() function.
Many algorithms will involve creating composite functions that combine these more basic operations. Additionally, we’ll need to look at a profound limitation of working with iterators and generator functions.
Here’s an example of this limitation:
>>> typical_iterator = iter([0, 1, 2, 3, 4])
>>> sum(typical_iterator)
10
>>> sum(typical_iterator)
0
We created an iterator...