Using reduce to transform collections
Sequences and functions that operate on sequences preserve the sequential ordering between elements. Lazy sequences avoid the unnecessary realization of elements in a collection until they are required for a computation, but the realization of these values is still performed in a sequential manner. However, this characteristic of sequential ordering may not be desirable for all computations performed over it. For example, it's not possible to map a function over a vector and then lazily realize values in the resulting collection out of order; since the map
function converts the supplied collection into a sequence. Also, functions such as map
and filter
are lazy, but still sequential by nature.
What's wrong with sequences?
One of the limitations of sequences is that they are realized in chunks. Let's study a simple example to illustrate what this means. Consider a unary function, as shown in Example 3.1, which we intend to map over a given...