reduce
accumulate a new value from all values in a list or dict
(any xs, any init, function f) -> any
Expects xs
to be a list
or a dict
.
Sets the initial accumulator value to init
, iterates through all xs
, and calls f
for each x
.
If f
accepts two arguments, the current accumulator value and x
are passed. If f
accepts three arguments, the current accumulator value, x
, and the index or key of x
are passed.
After each call the return value of f
becomes the new accumulator value.
Returns final accumulator value after iteration over xs
is completed.
Returns nil
if xs
is nil
.
Throws an error if xs
is neither a list nor a dict.
Throws an error if f
is nil.
Parameters
Name | Type | Description |
---|---|---|
xs |
list or dict | The list or dict to transform |
init |
any | Initial accumulator value |
f |
function |
A function of the form (any a, any x, any k) -> any that returns the new accumulator value for current accumulator value a , and item x at key or index k . |