🏃‍♂️ Takes the first N elements of an iterable.

Syntax

TypeScript
import { takeIter } from '@opentf/std';
takeIter<T>(
  iterable: Iterable<T>,
  n: number
): Generator<T>;

Parameters

  • iterable: The source iterable.

  • n: The number of elements to take.

Returns

A Generator that yields the specified number of elements, or all of them if the source is shorter.

Exactly n items are read from the source and no more, so a value the result does not include is never produced — which matters when producing one is expensive or has a side effect. An n of zero or less reads nothing at all.

Examples

TypeScript
const arr = [1, 2, 3, 4];
const iter = takeIter(arr, 2);
[...iter]; //=> [1, 2]
Last updated on
Edit this page