Skip to content

take

take<T>(iterable, count): IterableIterator<T>

Defined in: packages/core/src/iterable/take.ts:17

Yields at most the first count elements, then stops.

What Array.prototype.slice(0, count) does for an array, without materialising, and without needing to know the length up front. The source is closed as soon as the quota is met, so taking from an infinite iterable terminates.

Time complexity: O(count).

T

Iterable<T>

The input iterable.

number

How many elements to yield. Must be a non-negative integer; zero yields nothing and consumes nothing.

IterableIterator<T>

An iterator over the leading elements.

If count is not a non-negative integer.