Skip to content

Iterable

Works with JavaScript values that implement [Symbol.iterator].

Iterables include arrays, strings, generators, sets, and custom lazy sequences. The helpers in this module let code transform, search, group, and fold iterable values while preserving the input as an iterable instead of forcing an array first.

50 exports Added in v2.0.0 Source

Combining

append

Added in v2.0.0 Source

Appends an element to the end of an Iterable, creating a new Iterable.

When to use

Use to add one element after all elements of an iterable while keeping the result as a lazy Iterable.

Details

The result yields every element from self first, then yields last after self is exhausted.

Gotchas

If self is infinite or never completes, the appended element is never reached.

See

  • prepend for adding one element before the existing elements
  • appendAll for appending all elements from another iterable

Signature

declare const append: {
  <B>(last: B): <A>(self: Iterable<A>) => Iterable<B | A>;
  <A, B>(self: Iterable<A>, last: B): Iterable<A | B>;
};

appendAll

Added in v2.0.0 Source

Concatenates two iterables, combining their elements.

When to use

Use to lazily concatenate two iterables while preserving order, yielding all elements from self before that.

Details

The result is lazy. The iterator for that is not created or read until self is exhausted.

Gotchas

If self is infinite or never completes, that is never reached.

See

  • append for appending one value instead of another iterable
  • prependAll for yielding another iterable before self

Signature

declare const appendAll: {
  <B>(that: Iterable<B>): <A>(self: Iterable<A>) => Iterable<B | A>;
  <A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<A | B>;
};

cartesian

Added in v2.0.0 Source

Zips this Iterable crosswise with the specified Iterable.

Signature

declare const cartesian: {
  <B>(that: Iterable<B>): <A>(self: Iterable<A>) => Iterable<[A, B]>;
  <A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<[A, B]>;
};

Zips this Iterable crosswise with the specified Iterable using the specified combiner.

Signature

declare const cartesianWith: {
  <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>;
  <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>;
};

intersperse

Added in v2.0.0 Source

Places a separator between members of an Iterable.

When to use

Use to lazily insert a separator between adjacent values.

Details

If the input is a non-empty array, the result is also a non-empty array.

Signature

declare const intersperse: {
  <B>(middle: B): <A>(self: Iterable<A>) => Iterable<B | A>;
  <A, B>(self: Iterable<A>, middle: B): Iterable<A | B>;
};

prepend

Added in v2.0.0 Source

Prepends an element to the front of an Iterable, creating a new Iterable.

Signature

declare const prepend: {
  <B>(head: B): <A>(self: Iterable<A>) => Iterable<B | A>;
  <A, B>(self: Iterable<A>, head: B): Iterable<A | B>;
};

prependAll

Added in v2.0.0 Source

Prepends the specified prefix iterable to the beginning of the specified iterable.

Signature

declare const prependAll: {
  <B>(that: Iterable<B>): <A>(self: Iterable<A>) => Iterable<B | A>;
  <A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<A | B>;
};

Constructors

empty

Added in v2.0.0 Source

Creates an empty iterable that yields no elements.

When to use

Use when you need an empty iterable as a typed "no data" value or a base case for iterable operations.

Signature

declare function empty<A = never>(): Iterable<A>;

forever

Added in v4.0.0 Source

Repeats an iterable without an upper bound.

When to use

Use to cycle a reusable iterable without an upper bound when a downstream consumer controls how many values are taken.

Gotchas

The returned iterable is lazy and should usually be bounded with take or another terminating consumer before materializing it.

See

  • repeat for repeating an iterable a specific number of times
  • take for bounding the unbounded result before materializing it

Signature

declare function forever<A>(self: Iterable<A>): Iterable<A>;

makeBy

Added in v2.0.0 Source

Creates an iterable by applying a function to consecutive integers.

Details

The function is called with each index starting from 0. If no length is specified, the iterable is infinite. This is useful for generating sequences, patterns, or any indexed data.

Signature

declare function makeBy<A>(
  f: (i: number) => A,
  options?: {
    readonly length?: number;
  },
): Iterable<A>;

of

Added in v2.0.0 Source

Creates an iterable containing a single element.

When to use

Use to wrap a single value in an iterable context so it can be combined with other iterable operations.

Signature

declare function of<A>(a: A): Iterable<A>;

range

Added in v2.0.0 Source

Returns an iterable of integers starting at start and increasing by 1.

Details

When end is provided and start <= end, both endpoints are included. When end is omitted, the iterable is unbounded. When start > end, the iterable contains only start.

Signature

declare function range(start: number, end?: number): Iterable<number>;

repeat

Added in v4.0.0 Source

Repeats an iterable n times, yielding the full contents of self for each repetition.

When to use

Use to repeat an iterable's contents a specific number of times.

Details

The result is lazy. Each repetition obtains a new iterator from self.

See

  • forever for repeating without an upper bound
  • replicate for repeating a single value

Signature

declare const repeat: {
  (n: number): <A>(self: Iterable<A>) => Iterable<A>;
  <A>(self: Iterable<A>, n: number): Iterable<A>;
};

replicate

Added in v2.0.0 Source

Returns a Iterable containing a value repeated the specified number of times.

Details

n is normalized to an integer greater than or equal to 1.

Signature

declare const replicate: {
  (n: number): <A>(a: A) => Iterable<A>;
  <A>(a: A, n: number): Iterable<A>;
};

unfold

Added in v2.0.0 Source

Generates an iterable by repeatedly applying a function that produces the next element and state.

Details

This is useful for creating iterables from a generating function that maintains state. The function should return Option.some([value, nextState]) to continue or Option.none() to stop.

Signature

declare function unfold<B, A>(b: B, f: (b: B) => Option<readonly [A, B]>): Iterable<A>;

Converting

fromRecord

Added in v2.0.0 Source

Takes a record and returns an Iterable of tuples containing its keys and values.

Signature

declare function fromRecord<K extends string, A>(self: Readonly<Record<K, A>>): Iterable<[K, A]>;

Filtering

Deduplicates adjacent elements that are identical.

Signature

declare const dedupeAdjacent: <A>(self: Iterable<A>) => Iterable<A>;

Deduplicates adjacent elements that are identical using the provided isEquivalent function.

Signature

declare const dedupeAdjacentWith: {
  <A>(isEquivalent: (self: A, that: A) => boolean): (self: Iterable<A>) => Iterable<A>;
  <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Iterable<A>;
};

filter

Added in v2.0.0 Source

Filters an iterable to only include elements that match a predicate.

Details

This function creates a new iterable containing only the elements for which the predicate function returns true. Like map, this operation is lazy and elements are only tested when the iterable is consumed.

Signature

declare const filter: {
  <A, B>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Iterable<B>;
  <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => Iterable<A>;
  <A, B>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Iterable<B>;
  <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): Iterable<A>;
};

filterMap

Added in v2.0.0 Source

Transforms elements of an iterable using a function that returns a Result, keeping only successful values.

Details

This combines mapping and filtering in a single operation. The function is applied to each element, and only elements that result in Result.succeed are included in the result.

Signature

declare const filterMap: {
  <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>;
  <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>;
};

Transforms all elements of the Iterable for as long as the specified function succeeds.

Signature

declare const filterMapWhile: {
  <A, B, X>(f: (input: A, i: number) => Result<B, X>): (self: Iterable<A>) => Iterable<B>;
  <A, B, X>(self: Iterable<A>, f: (input: A, i: number) => Result<B, X>): Iterable<B>;
};

getFailures

Added in v4.0.0 Source

Returns a lazy iterable containing the failure values from an iterable of Results, skipping successful results.

Signature

declare function getFailures<R0, L>(self: Iterable<Result<R0, L>>): Iterable<L>;

getSomes

Added in v2.0.0 Source

Retrieves the Some values from an Iterable of Options.

Signature

declare function getSomes<A>(self: Iterable<Option<A>>): Iterable<A>;

getSuccesses

Added in v4.0.0 Source

Returns a lazy iterable containing the success values from an iterable of Results, skipping failed results.

Signature

declare function getSuccesses<R0, L>(self: Iterable<Result<R0, L>>): Iterable<R0>;

Folding

countBy

Added in v3.16.0 Source

Computes how many elements of the iterable pass the given predicate.

Signature

declare const countBy: {
  <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => number;
  <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): number;
};

reduce

Added in v2.0.0 Source

Reduces an iterable to a single value by applying a function to each element and accumulating the result.

Details

This function applies a reducing function against an accumulator and each element of the iterable (from left to right) to reduce it to a single value.

Signature

declare const reduce: {
  <B, A>(b: B, f: (b: B, a: A, i: number) => B): (self: Iterable<A>) => B;
  <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A, i: number) => B): B;
};

scan

Added in v2.0.0 Source

Reduces an Iterable from the left, keeping all intermediate results instead of only the final result.

Signature

declare const scan: {
  <B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<A>) => Iterable<B>;
  <A, B>(self: Iterable<A>, b: B, f: (b: B, a: A) => B): Iterable<B>;
};

Getters

drop

Added in v2.0.0 Source

Drops a max number of elements from the start of an Iterable

Details

n is normalized to a non-negative integer.

Signature

declare const drop: {
  (n: number): <A>(self: Iterable<A>) => Iterable<A>;
  <A>(self: Iterable<A>, n: number): Iterable<A>;
};

headUnsafe

Added in v4.0.0 Source

Gets the first element of an Iterable without returning an Option.

When to use

Use when the Iterable is known to be non-empty and direct access to the first element is preferred over handling Option.none.

Gotchas

Throws if the Iterable is empty.

Signature

declare function headUnsafe<A>(self: Iterable<A>): A;

size

Added in v2.0.0 Source

Returns the number of elements in a Iterable.

Signature

declare function size<A>(self: Iterable<A>): number;

take

Added in v2.0.0 Source

Keeps only a max number of elements from the start of an Iterable, creating a new Iterable.

Details

n is normalized to a non-negative integer.

Signature

declare const take: {
  (n: number): <A>(self: Iterable<A>) => Iterable<A>;
  <A>(self: Iterable<A>, n: number): Iterable<A>;
};

takeWhile

Added in v2.0.0 Source

Takes the longest initial Iterable prefix for which all elements satisfy the specified predicate.

Signature

declare const takeWhile: {
  <A, B>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Iterable<B>;
  <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => Iterable<A>;
  <A, B>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Iterable<B>;
  <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): Iterable<A>;
};

Grouping

group

Added in v2.0.0 Source

Groups equal, consecutive elements of an Iterable into NonEmptyArrays.

Signature

declare const group: <A>(self: Iterable<A>) => Iterable<NonEmptyArray<A>>;

groupBy

Added in v2.0.0 Source

Groups all elements by the string or symbol key returned by f.

Details

Each property in the returned record contains a non-empty array of elements that produced that key. Unlike group, matching elements do not need to be consecutive.

Signature

declare const groupBy: {
  <A, K extends string | symbol>(
    f: (a: A) => K,
  ): (self: Iterable<A>) => Record<Record.ReadonlyRecord.NonLiteralKey<K>, NonEmptyArray<A>>;
  <A, K extends string | symbol>(
    self: Iterable<A>,
    f: (a: A) => K,
  ): Record<Record.ReadonlyRecord.NonLiteralKey<K>, NonEmptyArray<A>>;
};

groupWith

Added in v2.0.0 Source

Groups equal, consecutive elements of an Iterable into NonEmptyArrays using the provided isEquivalent function.

Signature

declare const groupWith: {
  <A>(
    isEquivalent: (self: A, that: A) => boolean,
  ): (self: Iterable<A>) => Iterable<[A, ...Array<A>]>;
  <A>(self: Iterable<A>, isEquivalent: (self: A, that: A) => boolean): Iterable<[A, ...Array<A>]>;
};

Guards

isEmpty

Added in v2.0.0 Source

Checks whether an Iterable is empty.

Signature

declare function isEmpty<A>(self: Iterable<A>): self is Iterable<never, any, any>;

Mapping

map

Added in v2.0.0 Source

Transforms each element of an iterable using a function.

Details

This is one of the most fundamental operations for working with iterables. It applies a transformation function to each element, creating a new iterable with the transformed values. The operation is lazy, so elements are only transformed when the iterable is consumed.

Signature

declare const map: {
  <A, B>(f: (a: NoInfer<A>, i: number) => B): (self: Iterable<A>) => Iterable<B>;
  <A, B>(self: Iterable<A>, f: (a: NoInfer<A>, i: number) => B): Iterable<B>;
};

Predicates

contains

Added in v2.0.0 Source

Checks whether an iterable contains a value using Effect's default Equal equivalence.

Details

Can be called as contains(self, value) or curried as contains(value)(self).

Signature

declare const contains: {
  <A>(a: A): (self: Iterable<A>) => boolean;
  <A>(self: Iterable<A>, a: A): boolean;
};

containsWith

Added in v2.0.0 Source

Returns a function that checks if an Iterable contains a given value using a provided isEquivalent function.

Signature

declare function containsWith<A>(isEquivalent: (self: A, that: A) => boolean): {
  (a: A): (self: Iterable<A>) => boolean;
  (self: Iterable<A>, a: A): boolean;
};

some

Added in v2.0.0 Source

Checks whether a predicate holds true for some Iterable element.

Signature

declare const some: {
  <A>(predicate: (a: A, i: number) => boolean): (self: Iterable<A>) => boolean;
  <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): boolean;
};

Searching

findFirst

Added in v2.0.0 Source

Returns the first element that satisfies the specified predicate, or None if no such element exists.

Signature

declare const findFirst: {
  <A, B>(f: (a: NoInfer<A>, i: number) => Option<B>): (self: Iterable<A>) => Option<B>;
  <A, B>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Option<B>;
  <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => Option<A>;
  <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option<B>): Option<B>;
  <A, B>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option<B>;
  <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): Option<A>;
};

findLast

Added in v2.0.0 Source

Finds the last element for which a predicate holds.

Signature

declare const findLast: {
  <A, B>(f: (a: NoInfer<A>, i: number) => Option<B>): (self: Iterable<A>) => Option<B>;
  <A, B>(refinement: (a: NoInfer<A>, i: number) => a is B): (self: Iterable<A>) => Option<B>;
  <A>(predicate: (a: NoInfer<A>, i: number) => boolean): (self: Iterable<A>) => Option<A>;
  <A, B>(self: Iterable<A>, f: (a: A, i: number) => Option<B>): Option<B>;
  <A, B>(self: Iterable<A>, refinement: (a: A, i: number) => a is B): Option<B>;
  <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): Option<A>;
};

Sequencing

flatMap

Added in v2.0.0 Source

Applies a function to each element in an Iterable and returns a new Iterable containing the concatenated mapped elements.

Signature

declare const flatMap: {
  <A, B>(f: (a: NoInfer<A>, i: number) => Iterable<B>): (self: Iterable<A>) => Iterable<B>;
  <A, B>(self: Iterable<A>, f: (a: NoInfer<A>, i: number) => Iterable<B>): Iterable<B>;
};

Transforms elements using a function that may return null or undefined, filtering out the null/undefined results.

When to use

Use when working with APIs or functions that return nullable values, providing a clean way to filter out null or undefined while transforming.

Signature

declare const flatMapNullishOr: {
  <A, B>(f: (a: A) => B): (self: Iterable<A>) => Iterable<NonNullable<B>>;
  <A, B>(self: Iterable<A>, f: (a: A) => B): Iterable<NonNullable<B>>;
};

flatten

Added in v2.0.0 Source

Flattens an Iterable of Iterables into a single Iterable

Signature

declare function flatten<A>(self: Iterable<Iterable<A, any, any>>): Iterable<A>;

Splitting

chunksOf

Added in v2.0.0 Source

Splits an Iterable into length-n pieces. The last piece will be shorter if n does not evenly divide the length of the Iterable.

Signature

declare const chunksOf: {
  (n: number): <A>(self: Iterable<A>) => Iterable<Array<A>>;
  <A>(self: Iterable<A>, n: number): Iterable<Array<A>>;
};

Traversing

forEach

Added in v2.0.0 Source

Iterates over the Iterable, applying f to each element.

Signature

declare const forEach: {
  <A>(f: (a: A, i: number) => void): (self: Iterable<A>) => void;
  <A>(self: Iterable<A>, f: (a: A, i: number) => void): void;
};

Zipping

zip

Added in v2.0.0 Source

Takes two Iterables and returns an Iterable of corresponding pairs.

Signature

declare const zip: {
  <B>(that: Iterable<B>): <A>(self: Iterable<A>) => Iterable<[A, B]>;
  <A, B>(self: Iterable<A>, that: Iterable<B>): Iterable<[A, B]>;
};

zipWith

Added in v2.0.0 Source

Applies a function to pairs of elements at the same index in two Iterables, collecting the results. If one input Iterable is short, excess elements of the longer Iterable are discarded.

Signature

declare const zipWith: {
  <B, A, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>;
  <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>;
};