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 = ; // logs numbers 0, 1, ..., 9 one per linefor const x of console; // the sameLaziness ;
{ 1; 1; const fib1 fib2 = Laziness; fib1;} // 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
API
Table of Contents
- INFINITE ITERATORS
- cycle
- range
- repeat
- UTILITIES
- filter
- forEach
- map
- map2
- slice
- tail
- tee
- LAZINESS WRAPPER
- Laziness
INFINITE ITERATORS
Infinite Iterators
cycle
Generates all elements of the iterable. Once the original iterable is exhausted, yields all elements again. Repeats indefinitely.
Parameters
iter
Iterable<T>
Examples
// 'A', 'B', 'C', 'A', 'B', 'C', ...
Returns Generator<T, void, void>
range
Parameters
Examples
// 0, 1, 2, ...
Returns Generator<number, void, void>
repeat
Repeats value endlessly or up to limit times.
Parameters
value
Tlimit
number (optional, defaultInfinity
)
Examples
// 5, 5, 5, 5, ...
// 10, 10, 10
Returns Generator<T, void, void>
UTILITIES
Basic functions
filter
Analogical to Array.prototype.filter
Parameters
iter
Iterable<T>callback
function (T): boolean
Returns Generator<T, void, void>
forEach
Analogical to Array.prototype.forEach
Parameters
iter
Iterable<T>callback
function (T): void
Returns void
map
Analogical to Array.prototype.map
Parameters
iter
Iterable<T>callback
function (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
callback
function (U, V): Titer1
Iterable<U>iter2
Iterable<V>
Examples
// 11, 22, 33
Returns Generator<T, void, void>
slice
Analogical to Array.prototype.slice
Parameters
Returns Generator<T, void, void>
tail
Skips first element of iterable
Parameters
iter
Iterable<T>
Returns Generator<T, void, void>
tee
Clone interable into n independent instances
Parameters
iter
Iterable<T>n
number (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
iter
Iterable<T>
Examples
Array // 1, 2, 3
{ 1; 1; const fib1 fib2 = Laziness; fib1;} // 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
filter
See filter
Parameters
callback
function (T): boolean
Returns Laziness<T>
forEach
See forEach
Parameters
callback
function (T): void
map
See map
Parameters
callback
function (T): U
Returns Laziness<U>
map2
See map2
Parameters
callback
function (T): Uiter2
Iterable<V>
Returns Laziness<U>
slice
See slice
Parameters
Returns Laziness<T>
tail
See tail
Returns Laziness<T>
tee
See tee
Parameters
n
number (optional, default2
)
toArray
Returns Array<T>
from
Same as new Laziness(iter)
Parameters
iter
Iterable<T>
Returns Laziness<T>