Reducing to a Value

The reduce function reduces a collection to a value by repeatedly applying a function to an accumulated value and the next element in the collection, using an optional initial value. The into function is a special case that reduces a collection to another collection instead of a simple value.

For example, in our space simulation, consider computing the total number of moons across all planets in the solar system. We need to first extract the number of moons for each planet (a mapping transformation), then reduce those to a single value (the total) by using the + function. The reduce function is often used to combine a collection transformation with a reduction step.

We can compute the total for a collection of planets using ...

Get Clojure Applied now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.