Notable Pottery Manufacturer

    laziness

    1.0.8 • Public • Published

    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

    • iter Iterable<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

    • value T
    • limit number (optional, default Infinity)

    Examples

    repeat(5) // 5, 5, 5, 5, ...
    repeat(10, 3) // 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): T
    • iter1 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>

    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, default 2)

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

    filter

    See filter

    Parameters

    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): U
    • iter2 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, default 2)

    Returns Array<Laziness<T>>

    toArray

    Returns Array<T>

    from

    Same as new Laziness(iter)

    Parameters

    • iter Iterable<T>

    Returns Laziness<T>

    Install

    npm i laziness

    DownloadsWeekly Downloads

    1

    Version

    1.0.8

    License

    MIT

    Last publish

    Collaborators

    • mhagmajer