Aller au contenu principal

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: Iterable<Value, any, any>): Int
  • Returns the size of iterable

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

    Type parameters

    • Value

Constructor

create

  • create<Value>(iteratorFn: () => Iterator<Value, any, any>): Iterable<Value>
  • Iterable constructor

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

    Type parameters

    • Value

empty

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

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

    Type parameters

    • Value = never

generate

  • generate<Value>(length: number, mapFn: (index: Int) => Value): 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)

    Type parameters

    • Value

of

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

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

    Type parameters

    • Value

Predicate

some

  • some<Value>(self: Iterable<Value, any, any>, predicate: (currentValue: Value, currentIndex: Int) => boolean): 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 parameters

    • Value

Type

hasInstance

  • hasInstance(anyValue: unknown): 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

every

  • every<Value>(self: Iterable<Value, any, any>, predicate: (currentValue: Value, currentIndex: Int) => boolean): 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

    Type parameters

    • Value

filter

  • filter<Value>(self: Iterable<Value, any, any>, predicate: (currentValue: Value, currentIndex: Int) => boolean): 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)

    Type parameters

    • Value

find

  • find<Value>(self: Iterable<Value, any, any>, predicate: (currentValue: Value, currentIndex: number) => boolean): 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

    Type parameters

    • Value

flatMap

  • flatMap<ValueFrom, ValueTo>(self: Iterable<ValueFrom, any, any>, mapFn: (currentValue: ValueFrom, currentIndex: Int) => Iterable<ValueTo, any, any>): 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)

    Type parameters

    • ValueFrom
    • ValueTo

map

  • map<ValueFrom, ValueTo>(self: Iterable<ValueFrom, any, any>, mapFn: (currentValue: ValueFrom, currentIndex: Int) => ValueTo): 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)

    Type parameters

    • ValueFrom
    • ValueTo

reduce

  • reduce<Value, Return>(self: Iterable<Value, any, any>, reducer: (accumulator: Return, currentValue: Value, currentIndex: Int) => Return, initialValue: Return): Return
  • Reduce an initialValue to the reducer function

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

    Type parameters

    • Value
    • Return

zip

  • zip<L, R>(left: Iterable<L, any, any>, right: Iterable<R, any, any>): 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'])

    Type parameters

    • L
    • R