@ndhoule/foldl
Version:
Apply a function to each value in a collection, accumulating the results into a single return value.
41 lines (28 loc) • 1.1 kB
Markdown
Apply a function to each value in a collection, accumulating the results into a single return value.
```sh
$ component install ndhoule/foldl
$ npm install @ndhoule/foldl
```
Reduces all the values in a collection down into a single value. Does so by iterating through the collection from left to right, repeatedly calling an `iterator` function and passing to it four arguments: `(accumulator, value, index, collection)`.
```javascript
foldl(function(total, n) {
return total + n;
}, 0, [1, 2, 3]);
//=> 6
var phonebook = { bob: '555-111-2345', tim: '655-222-6789', sheila: '655-333-1298' };
foldl(function(results, phoneNumber) {
if (phoneNumber[0] === '6') {
return results.concat(phoneNumber);
}
return results;
}, [], phonebook);
// => ['655-222-6789', '655-333-1298']
```
Released under the [MIT license](LICENSE.md).
[]: https://travis-ci.org/ndhoule/foldl
[]: https://travis-ci.org/ndhoule/foldl.svg?branch=master