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

from

  • from<Value>(source: Iterable<Value, any, any> | () => Iterator<Value, any, any>): 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)

    Type parameters

    • Value

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

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

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

chunks

  • chunks<Value>(self: Iterable<Value, any, any>, chunkSize: number): 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])

    Type parameters

    • Value

concat

  • concat<Value>(iterables: Iterable<Iterable<Value, any, any>, any, any>): 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)

    Type parameters

    • Value

drop

  • drop<Value>(self: Iterable<Value, any, any>, limit: number): 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)

    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

take

  • take<Value>(self: Iterable<Value, any, any>, limit: number): 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)

    Type parameters

    • Value

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