0.1.1 • Published 8 years ago

@panosoft/co-ramda-utils v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

co-ramda-utils

Travis

Utilities built on top of co and Ramda to support common functional iterators with generators as callbacks instead of simple functions.

Installation

npm install @panosoft/co-ramda-utils

Usage

var cRu = require('@panosoft/co-ramda-utils');

API


filterG(genFn, list)

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.
  • 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.

yield cRu.filterG(function* (item) {
	return yield Promise.resolve(item % 2);
}, [1, 2, 3, 4, 5, 6])); // [1, 3, 5]

forEachG(genFn, list)

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.
  • list - List to iterate over.

Example

In this example, the Promise.resolve(item) would normally be an async function that yields a Promise.

var i = 0;
yield cRu.forEachG(function* (item) {
	i = yield Promise.resolve(item);
}, [1, 2, 3]); // i = 3

mapG(genFn, list)

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.
  • 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.

cRu.mapG(function* (item) {
	return (yield Promise.resolve(item)) * 10;
}, [1, 2, 3])); // [10, 20, 30]

reduceG(genFn, acc, list)

Reduce list where callback is an async function.

Arguments

  • genFn - A Generator Function which may ONLY yield a yieldable.
  • 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.

cRu.reduceG(function* (acc, item) {
	return acc + (yield Promise.resolve(item));
}, 0, [1, 2, 3])); // 6