Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ndhoule/foldl
Apply a function to each value in a collection, accumulating the results into a single return value.
https://github.com/ndhoule/foldl
Last synced: 13 days ago
JSON representation
Apply a function to each value in a collection, accumulating the results into a single return value.
- Host: GitHub
- URL: https://github.com/ndhoule/foldl
- Owner: ndhoule
- License: mit
- Created: 2015-02-26T19:55:24.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-05-02T20:30:06.000Z (over 8 years ago)
- Last Synced: 2024-10-05T08:47:58.196Z (about 1 month ago)
- Language: JavaScript
- Size: 14.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
- License: LICENSE.md
Awesome Lists containing this project
README
# foldl [![CI][ci-badge]][ci-link]
Apply a function to each value in a collection, accumulating the results into a single return value.
## Installation
```sh
$ component install ndhoule/foldl
$ npm install @ndhoule/foldl
```## API
### `reduce(iterator : Function, accumulator : *, collection : Array|Object)`
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]);
//=> 6var 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']
```## License
Released under the [MIT license](LICENSE.md).
[ci-link]: https://travis-ci.org/ndhoule/foldl
[ci-badge]: https://travis-ci.org/ndhoule/foldl.svg?branch=master