1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00
Files
til/clojure/reductions.md
2015-08-16 14:27:21 -05:00

675 B

Reductions

One of the core functions in Clojure is reduce. It allows you to build up some result based on applying a function to each value in a collection. Clojure provides a similar function that builds up a (lazy) sequence of intermediate values as it is performing a reduce. This function is reductions.

Using reduce to sum a collection of integers looks like this

> (reduce + 0 [1 2 3 4 5])
=> 15

whereas reductions performing the same task will look like this

> (reductions + 0 [1 2 3 4 5])
=> (0 1 3 6 10 15)

h/t Josh Davey