Clojure 1.5 introduced the clojure.core.reducers
library. This library provides a lot of interesting and exciting features, including composing multiple calls to map
and other sequence-processing high-order functions and abstracting map
and other
functions for different types of collections while maintaining the collection type.
Looking at the following chart, initial operations on individual data items such as map
and filter
operate on items of the original dataset. Then the output of the operations on the items are combined using a reduce function. Finally, the outputs of the reduction step are progressively combined until the final result is produced. This could involve a reduce-type operation such as addition, or an accumulation, such as the into
function.
Another feature of reducers is that they can automatically partition and parallelize the processing of tree-based data structures. This includes Clojure's native vectors and hash maps.
For this recipe, we...