array.reduce(0, :+)
While equivalent to `array.inject(0, :+)`, the term **reduce** is entering a more common vernacular with the rise of [MapReduce programming models][1].
**inject**, **reduce**, **fold**, **accumulate**, and **compress** are all synonymous as a class of [folding functions][2]. I find consistency across your code base most important, but since various communities tend to prefer one word over another, it’s nonetheless useful to know the alternatives.
To emphasize the map-reduce verbiage, here’s a version that is a little bit more forgiving on what ends up in that array.
array.map(&:to_i).reduce(0, :+)
Some additional relevant reading:
-
[To see links please register here]
-
[To see links please register here]
-
[To see links please register here]
[1]:
[To see links please register here]
[2]:
[To see links please register here]