laziness
v1.0.8
Published
Lazy evaluation utilities for JavaScript powered by ES6 iterators and generators
Downloads
27
Maintainers
Readme
laziness
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.
Installation
npm install --save laziness
Usage example
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, ...API
Table of Contents
INFINITE ITERATORS
Infinite Iterators
cycle
Generates all elements of the iterable. Once the original iterable is exhausted, yields all elements again. Repeats indefinitely.
Parameters
iterIterable<T>
Examples
cycle('ABC') // 'A', 'B', 'C', 'A', 'B', 'C', ...Returns Generator<T, void, void>
range
Parameters
Examples
range(0, Infinity) // 0, 1, 2, ...Returns Generator<number, void, void>
repeat
Repeats value endlessly or up to limit times.
Parameters
valueTlimitnumber (optional, defaultInfinity)
Examples
repeat(5) // 5, 5, 5, 5, ...repeat(10, 3) // 10, 10, 10Returns Generator<T, void, void>
UTILITIES
Basic functions
filter
Analogical to Array.prototype.filter
Parameters
iterIterable<T>callbackfunction (T): boolean
Returns Generator<T, void, void>
forEach
Analogical to Array.prototype.forEach
Parameters
iterIterable<T>callbackfunction (T): void
Returns void
map
Analogical to Array.prototype.map
Parameters
iterIterable<T>callbackfunction (T): U
Returns Generator<U, void, void>
map2
Generates values of the function when applied arguments from each of the iterables. Stops when the shortest iterable is exhausted.
Parameters
callbackfunction (U, V): Titer1Iterable<U>iter2Iterable<V>
Examples
map2((x, y) => x + y, range(1, 4), range(10, 40, 10)) // 11, 22, 33Returns Generator<T, void, void>
slice
Analogical to Array.prototype.slice
Parameters
Returns Generator<T, void, void>
tail
Skips first element of iterable
Parameters
iterIterable<T>
Returns Generator<T, void, void>
tee
Clone interable into n independent instances
Parameters
iterIterable<T>nnumber (optional, default2)
Returns Array<Generator<T, void, void>>
LAZINESS WRAPPER
Handy wrapper for common operations on iterables.
Laziness
Convenient wrapper for chaining calls to library functions.
You can also use it as iterable.
Parameters
iterIterable<T>
Examples
Array.from(new Laziness([1, 2, 3])) // 1, 2, 3function* 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, ...filter
See filter
Parameters
callbackfunction (T): boolean
Returns Laziness<T>
forEach
See forEach
Parameters
callbackfunction (T): void
map
See map
Parameters
callbackfunction (T): U
Returns Laziness<U>
map2
See map2
Parameters
callbackfunction (T): Uiter2Iterable<V>
Returns Laziness<U>
slice
See slice
Parameters
Returns Laziness<T>
tail
See tail
Returns Laziness<T>
tee
See tee
Parameters
nnumber (optional, default2)
toArray
Returns Array<T>
from
Same as new Laziness(iter)
Parameters
iterIterable<T>
Returns Laziness<T>
