Reducing collections
When you map collections, you always end up with the same type of collection with the same number of values. When you iterate over every value in the collection and produce something that's not a mapped collection, it's called a reduction. Filtering is a type of reduction, for example, but sometimes, you need to produce noncollection values from collections.
When filtering isn't enough
If you need to produce another collection, combining filter()
and map()
is usually the right answer. You can compose your filter()
and map()
calls using concise and easy to read iteratee functions. When you need to produce a simple value, the reduce()
method is there for you.
Note
Manually reducing collections using reduce()
requires more implementation effort than either map()
and filter()
, which is why you should avoid it where possible. Another reason to avoid it is that it can't be executed lazily in a sequence.
Producing minimums and maximums
An example of where mapping and filtering collections...