generator-utils
Utility functions for manipulating generators as lazy sequences.
Install
# via yarn $ yarn add generator-utils# via npm $ npm install generator-utils
Usage
In a node.js environment, e.g. import { filter } from 'generator-utils';
or const { filter } = require('generator-utils');
.
# combine(generators)
Creates a new generator yielding all in-order combinations of values from the given generators. All values from all generators will be read, so do not use this with infinite generators.
// [[0, 4], [0, 5], [1, 4], [1, 5]]
# concat(generators)
Returns a generator yielding all the values from the given generators in order. If any generators are infinite generators none of the values from subsequent generators will be read.
// [0, 1, 4, 5]
# filter(generator, transform)
Returns a generator that yields values from another generator passing a predicate. This may be used with infinite generators, though care should be taken to ensure that the predicate does not return false for all values.
// [0, 2, 4]
# filterMap(generator, transform)
Combines filter
and map
in one transform. Instead of returning false,
filtering is done by calling the skip
function passed as the second
argument to transform.
// [1, 9, 25]
# flatten(generator)
Makes a generator from a generator producing other generators.
// [2, 3, 4, 5, 6, 7]
# forEach(generator, iterator)
Calls a function for each value in a generator.
// prints "1\n2\n3\n4\n"
# fromArray(array)
Returns a generator yielding the values from the given array in order.
// [1, 2, 3]
# map(map, transform)
Maps one generator to another by passing all values through a transformer.
// [4, 6, 8, 10]
# range(min, max)
Returns a generator yielding values from min up to and including max.
// [8, 9, 10]
# take(generator, count)
Returns up to the first count values of a generator.
// [10, 11, 12]
# toArray(generator)
Reads all values from a generator and returns an array containing them. Be careful not to use this with infinite generators, as it will never return and your program will eventually run out of memory.
// [7, 8, 9]