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/sampleSize.md.

sampleSize (Functional Programming) es-toolkit

Creates a function that returns random values from an array. Use it with pipe.

const result = pipe(array, sampleSize(size));
Info

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

Usage

sampleSize returns size random values from the piped array without repeating the same array position.

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

const values = pipe([1, 2, 3, 4], sampleSize(2));
// values has length 2 and contains values from the input array.

Parameters

sizenumberrequired

The number of random values to return.

Returns

A function that maps a readonly T[] to random values.

(array: readonly T[]) => T[]

Throws

Throws an error if size is greater than the length of the piped array.

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.