Skip to content

flattenDeep

flattenDeep<T>(iterable): IterableIterator<T>

Defined in: packages/core/src/iterable/flatten-deep.ts:25

Lazily flattens nested iterables to any depth.

A string is never descended into, even though it is an Iterable<string> — flattening ["ab", ["cd"]] yields "ab" then "cd", not six characters. That is the case every deep-flatten gets wrong, and the reason this is not just flatten applied repeatedly. Every other iterable is descended into, including Set, Map and typed arrays.

Depth is bounded by the call stack, so a structure that contains itself overflows rather than yielding forever — the same way Array.prototype.flat behaves.

Time complexity: O(n) over the total number of leaves.

T

NestedIterable<T>

The nested iterable to flatten.

IterableIterator<T>

An iterator over the leaves, in order.