Lazy evaluation utilities for JavaScript powered by ES6 iterators and generators.
Well-tested and with Flow type definitions included.
Compatible with Node v6.11.2 LTS or later.
npm install --save laziness
const { default: Laziness, range } = require('laziness');
// logs numbers 0, 1, ..., 9 one per line
for (const x of range(0, 10)) {
console.log(x);
}
// the same
Laziness.from(range(0, 10))
.forEach(x => console.log(x));
function* fib() {
yield 1;
yield 1;
const [fib1, fib2] = Laziness.from(fib()).tee();
yield* fib1.map2((x, y) => x + y, fib2.tail());
}
fib() // 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
- INFINITE ITERATORS
- cycle
- range
- repeat
- UTILITIES
- filter
- forEach
- map
- map2
- slice
- tail
- tee
- LAZINESS WRAPPER
- Laziness
Infinite Iterators
Generates all elements of the iterable. Once the original iterable is exhausted, yields all elements again. Repeats indefinitely.
Parameters
iter
Iterable<T>
Examples
cycle('ABC') // 'A', 'B', 'C', 'A', 'B', 'C', ...
Returns Generator<T, void, void>
Parameters
Examples
range(0, Infinity) // 0, 1, 2, ...
Returns Generator<number, void, void>
Repeats value endlessly or up to limit times.
Parameters
value
Tlimit
number (optional, defaultInfinity
)
Examples
repeat(5) // 5, 5, 5, 5, ...
repeat(10, 3) // 10, 10, 10
Returns Generator<T, void, void>
Basic functions
Analogical to Array.prototype.filter
Parameters
iter
Iterable<T>callback
function (T): boolean
Returns Generator<T, void, void>
Analogical to Array.prototype.forEach
Parameters
iter
Iterable<T>callback
function (T): void
Returns void
Analogical to Array.prototype.map
Parameters
iter
Iterable<T>callback
function (T): U
Returns Generator<U, void, void>
Generates values of the function when applied arguments from each of the iterables. Stops when the shortest iterable is exhausted.
Parameters
callback
function (U, V): Titer1
Iterable<U>iter2
Iterable<V>
Examples
map2((x, y) => x + y, range(1, 4), range(10, 40, 10)) // 11, 22, 33
Returns Generator<T, void, void>
Analogical to Array.prototype.slice
Parameters
Returns Generator<T, void, void>
Skips first element of iterable
Parameters
iter
Iterable<T>
Returns Generator<T, void, void>
Clone interable into n independent instances
Parameters
iter
Iterable<T>n
number (optional, default2
)
Returns Array<Generator<T, void, void>>
Handy wrapper for common operations on iterables.
Convenient wrapper for chaining calls to library functions.
You can also use it as iterable.
Parameters
iter
Iterable<T>
Examples
Array.from(new Laziness([1, 2, 3])) // 1, 2, 3
function* fib() {
yield 1;
yield 1;
const [fib1, fib2] = Laziness.from(fib()).tee();
yield* fib1.map2((x, y) => x + y, fib2.tail());
}
fib() // 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
See filter
Parameters
callback
function (T): boolean
Returns Laziness<T>
See forEach
Parameters
callback
function (T): void
See map
Parameters
callback
function (T): U
Returns Laziness<U>
See map2
Parameters
callback
function (T): Uiter2
Iterable<V>
Returns Laziness<U>
See slice
Parameters
Returns Laziness<T>
See tail
Returns Laziness<T>
See tee
Parameters
n
number (optional, default2
)
Returns Array<T>
Same as new Laziness(iter)
Parameters
iter
Iterable<T>
Returns Laziness<T>