Skip to main content

Iterable

A collection of functions to manipulate Iterable

@example
const iterable = Iterable.create(() => ({
next() { ... }
}));

const iterable = Iterable.create(function* () { ... })

Index

Accessor

size

  • size<Value>(self): Int
  • Returns the size of iterable

    @example
    const iterable = Iterable.of(1, 2, 3);
    size(iterable); // 3

Constructor

create

  • create<Value>(iteratorFn): Iterable<Value>
  • Iterable constructor

    @example
    const iterable = Iterable.create(() => ({
    next() { ... }
    }))

empty

  • empty<Value>(): Iterable<Value>
  • Returns an iterable that have no value

    @example
    Array.from(Iterable.empty()) // == []

from

  • from<Value>(source): Iterable<Value>
  • @example
    Iterable.from([1, 2, 3]);// Iterable.of(1, 2, 3)
    Iterable.from(function* () { yield 1; yield 2; yield 3; })// Iterable.of(1, 2, 3)

generate

  • generate<Value>(length, mapFn): Iterable<Value>
  • Generate an iterable of length using mapFn(index) on each element

    @example
    Iterable.generate(3, () => 'a');// == Iterable.of('a', 'a', 'a')
    Iterable.generate(3, (index) => index * 2);// == Iterable.of(0, 2, 4)

of

  • of<Value>(...values): Iterable<Value>
  • Create an iterable of given values

    @example
    Iterable.of('a', 'b', 'c');// 'a', 'b', 'c'

Predicate

every

  • every<Value>(self, predicate): boolean
  • Tests whether all elements in the iterable pass the test implemented by the provided function.

    @example
    const iterable = Iterable.of(1, 2, 3);
    Iterable.every(iterable, (currentValue) => currentValue >= 1); // true
    Iterable.every(iterable, (currentValue) => currentValue >= 2); // false
    Iterable.every(iterable, (currentValue) => currentValue < 0); // false

some

  • some<Value>(self, predicate): boolean
  • Tests whether any element in the iterable pass the test implemented by the provided function.

    @example
    const iterable = Iterable.of(1, 2, 3);
    Iterable.some(iterable, (currentValue) => currentValue >= 1); // true
    Iterable.some(iterable, (currentValue) => currentValue >= 2); // true
    Iterable.some(iterable, (currentValue) => currentValue < 0); // false

Type

hasInstance

  • hasInstance(anyValue): anyValue is Iterable<unknown, any, any>
  • Returns true if anyValue is a valid Iterable

    @example
    Iterable.hasInstance({});// false
    Iterable.hasInstance([]);// true
    Iterable.hasInstance({ [Symbol.iterator]: () => ({ next: () => ({ done: true }) }) });// true

Other

chunks

  • chunks<Value>(self, chunkSize): Iterable<Value[]>
  • Splits an iterable into chunks of a specified size

    @example
    const chunks = Iterable.chunks([1, 2, 3, 4, 5], 2); // == Iterable.of([1, 2], [3, 4], [5])

concat

  • concat<Value>(iterables): Iterable<Value>
  • Concatenates multiple iterables into a single iterable

    @example
    const iterable = Iterable.of(
    Iterable.of(1, 2),
    Iterable.of(3, 4),
    );
    Iterable.concat(iterable); // == Iterable.of(1, 2, 3, 4)

drop

  • drop<Value>(self, limit): Iterable<Value>
  • Skips the given number of elements at the start of this iterator.

    @example
    const iterable = Iterable.of(1, 2, 3);
    Iterable.drop(iterable, 1); // == Iterable.of(2, 3)

filter

  • filter<Value>(self, predicate): Iterable<Value>
  • Return a new iterable that filters values using predicate

    @example
    const iterable = [1, 2, 3];
    Iterable.filter(
    iterable,
    (currentValue, currentIndex) => currentValue > 1,
    );// == Iterable.of(2, 3)

find

  • find<Value>(self, predicate): Option<Value>
  • Returns the first element for which the given function returns true, otherwise undefined.

    @example
    const iterable = Iterable.of(
    { name: 'amy', id: 1 },
    { name: 'bob', id: 2 }
    );
    find(iterable, (item) => item.name === 'bob') // { name: 'bob', id: 2 }
    find(iterable, (item) => item.name === 'cat') // undefined

flatMap

  • flatMap<ValueFrom, ValueTo>(self, mapFn): Iterable<ValueTo>
  • Return a new Iterable which applies mapFn and concatenate the result to the previous

    @example
    const iterable = Iterable.of(1, 2, 3);
    Iterable.flatMap(
    iterable,
    (currentValue, currentIndex) => Iterable.of(currentValue, currentValue * 2),
    );// == Iterable.of(1, 2, 2, 4, 3, 6)

map

  • map<ValueFrom, ValueTo>(self, mapFn): Iterable<ValueTo>
  • Return a new Iterable which applies mapFn to each values

    @example
    const iterable = [1, 2, 3];
    Iterable.map(
    iterable,
    (currentValue, currentIndex) => currentValue * 2,
    );// == Iterable.of(2, 4, 6)

reduce

  • reduce<Value, Return>(self, reducer, initialValue): Return
  • Reduce an initialValue to the reducer function

    @example
    const iterable = [1, 2, 3];
    Iterable.reduce(iterable, (total, currentValue, currentIndex) => total + value, 0);// 6

take

  • take<Value>(self, limit): Iterable<Value>
  • Take a specified number of elements from an iterable.

    @example
    const iterable = Iterable.of(1, 2, 3);
    Iterable.take(iterable, 2); // == Iterable.of(1, 2)

zip

  • zip<L, R>(left, right): Iterable<[L, R]>
  • Combine two iterables into an iterable of couple of their values. The result has the size of the smallest iterable used.

    @example
    const left = [1, 2, 3];
    const right = ['a', 'b'];
    Iterable.zip(left, right);// == Iterable.of([1, 'a'], [2, 'b'])