1.0.0 • Published 6 years ago
reduce-iterable v1.0.0
reduce-iterable
Applies a function to iterated values to reduce them to a single value. It’s like Array.prototype.reduce()
but it works for any iterable object.
Installation
Requires Node.js 6.0.0 or above.
npm i reduce-iterable
API
The module exports a single function.
Parameters
- Bindable:
iter
(iterable) cb
(Function): A callback which is called for each iterated value.- Parameters
acc
(any): The accumulator.x
(any): The iterated value.i
(integer): The number of values iterated so far. Starts at0
if the accumulator was given a starting value; otherwise starts at1
.iter
(iterable): The originaliter
.
- Return Value: The accumulator that should be passed to the next iteration.
- Parameters
- Optional:
acc
(any): The initial value of the accumulator. Defaults to the first iterated value.
Return Value
The final accumulator value.
Example
const reduce = require('reduce-iterable')
reduce([1, 2, 3], (acc, x) => acc + x) // 6
// Supports the bind operator
[1, 2, 3]::reduce((acc, x) => acc + x) // 6
Related
- filter-iter: Filters an iterable object so that it only yields values which pass a test function.
- partition-iterable: Divides iterated values into those that match a filter and those that don’t.
- unique-iterable: Filters an iterable object so it doesn’t yield the same value more than once.
- unique-iterable-by: Filters yielded values by testing uniqueness with an index, key, or callback.
1.0.0
6 years ago