For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/fp/take.md.

take (Functional Programming) es-toolkit

Creates a function that returns the first count elements of an array. Use it with pipe.

const result = pipe(array, take(count));
Info

Prefer the original es-toolkit take in ordinary code. Use this fp variant when composing transformations with pipe.

Usage

take returns the first count elements. If count is greater than the array length, the whole array is returned. For a non-negative integer count it is lazy-capable: inside a pipe it ends the walk as soon as count elements have been collected, so preceding lazy operations never process the rest of the input.

import { map, pipe, take } from 'es-toolkit/fp';

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

// `count` larger than the length returns the whole array.
pipe([1, 2, 3], take(5)); // => [1, 2, 3]

// Early termination: `map` only runs three times.
pipe([1, 2, 3, 4, 5], map(expensiveTransform), take(3));

Parameters

countnumberrequired

The number of elements to take from the front of the array. A negative count follows es-toolkit's take and drops from the end instead.

Returns

A function that maps a readonly T[] to a new T[] with at most count elements.

(array: readonly T[]) => T[]
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.