yta
    Preparing search index...

    Function take

    • Take a specific number of items from the front sequence and drop the rest.

      import { pipe } from "yta";
      import { range, take, toArray } from "yta/sync";

      pipe(range(15), take(5), toArray());
      // => [0, 1, 2, 3, 4]

      If the number is negative take the last n numbers of the sequence, which is effectively the same as drop(length - n) if you know the length of the length of the iterator.

      import { pipe } from "yta";
      import { range, take, toArray } from "yta/sync";

      pipe(range(15), take(-5), toArray());
      // => [10, 11, 12, 13, 14]

      Note: because the length of the iterator is not known—if you use negative index the entire iterator has to be consumed before yielding the first item. This can result in an infinite loop if the iterator is indefinite.

      See also:

      Type Parameters

      • A

        The item type

      Parameters

      • n: number

        The number of items to take

      Returns (items: Iterable<A>) => Generator<A, void>