Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/panosoft/co-ramda-utils
Utilities built on top of co and Ramda to support common functional iterators with generators as callbacks instead of simple functions.
https://github.com/panosoft/co-ramda-utils
Last synced: about 2 months ago
JSON representation
Utilities built on top of co and Ramda to support common functional iterators with generators as callbacks instead of simple functions.
- Host: GitHub
- URL: https://github.com/panosoft/co-ramda-utils
- Owner: panosoft
- Created: 2016-04-07T20:37:50.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-04-07T20:48:05.000Z (over 8 years ago)
- Last Synced: 2024-08-10T21:22:07.977Z (5 months ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 2
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# co-ramda-utils
[![Travis](https://img.shields.io/travis/panosoft/co-ramda-utils.svg)](https://travis-ci.org/panosoft/co-ramda-utils)
Utilities built on top of **co** and **Ramda** to support common functional iterators with generators as callbacks instead of simple functions.
## Installation
```sh
npm install @panosoft/co-ramda-utils
```## Usage
```js
var cRu = require('@panosoft/co-ramda-utils');
```## API
- [`filterG`](#filterG)
- [`forEachG`](#forEachG)
- [`mapG`](#mapG)
- [`reduceG`](#reduceG)---
Filter a list where the predicate is an **async** function.
__Arguments__
- `genFn` - A Generator Function that's a predicate which may ONLY yield a [`yieldable`](https://github.com/tj/co#yieldables).
- `list` - List to filter.__Returns__
A filtered list.
__Example__
In this example, the `Promise.resolve(item % 2)` would normally be an async function that yields a Promise.
```js
yield cRu.filterG(function* (item) {
return yield Promise.resolve(item % 2);
}, [1, 2, 3, 4, 5, 6])); // [1, 3, 5]```
---
Iterator over a list where callback is an async function and iteration **must** be done in order.
To execute the iteration in parallel, `R.map` could be used to return a list of Promises which would be yielded to `co.wrap` which will wait for all Promises to be resolved.
__Arguments__
- `genFn` - A Generator Function which may ONLY yield a [`yieldable`](https://github.com/tj/co#yieldables).
- `list` - List to iterate over.__Example__
In this example, the `Promise.resolve(item)` would normally be an async function that yields a Promise.
```js
var i = 0;
yield cRu.forEachG(function* (item) {
i = yield Promise.resolve(item);
}, [1, 2, 3]); // i = 3```
---
Map over a list where callback is an async function and iteration **must** be done in order.
To execute the iteration in parallel, `R.map` could be used to return a list of Promises which would be yielded to `co.wrap` which will wait for all Promises to be resolved returning the final mapped list.
__Arguments__
- `genFn` - A Generator Function which may ONLY yield a [`yieldable`](https://github.com/tj/co#yieldables).
- `list` - List to map over.__Returns__
A list of the same size.
__Example__
In this example, the `Promise.resolve(item)` would normally be an async function that yields a Promise.
```js
cRu.mapG(function* (item) {
return (yield Promise.resolve(item)) * 10;
}, [1, 2, 3])); // [10, 20, 30]```
---Reduce list where callback is an async function.
__Arguments__
- `genFn` - A Generator Function which may ONLY yield a [`yieldable`](https://github.com/tj/co#yieldables).
- `acc` - Initial accumulator value.
- `list` - List to map over.__Returns__
The final accumulator.
__Example__
In this example, the `Promise.resolve(item)` would normally be an async function that yields a Promise.
```js
cRu.reduceG(function* (acc, item) {
return acc + (yield Promise.resolve(item));
}, 0, [1, 2, 3])); // 6
```---