reduce()¶
Info
New in Atfinity 17.
Description¶
reduce() folds a list into a single value by threading an accumulator through the list, left to right. Unlike map() â which processes each element independently â reduce() lets each step build on the result of the previous one, so it can express running totals, products, and stateful checksums.
The arrow function takes two arguments: the accumulator and the current element. reduce() also takes an initial accumulator value, which is returned unchanged for an empty list.
Syntax¶
Returns: the final accumulator value.
Example¶
Returns 15 â the sum.
Returns 120 â the product.
Returns 'abc'.
Stateful checksums¶
reduce() can express checksums whose steps depend on the previous result â which map() cannot. For example, the ISO 7064 MOD 11,10 scheme (used by some national tax numbers) carries a running product across the digits: