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

sample (Functional Programming) es-toolkit

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

const result = pipe(array, sample());
Info

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

Usage

sample returns one random value from the piped array.

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

const value = pipe([1, 2, 3], sample());
// value is one of 1, 2, or 3.

Parameters

This function takes no arguments; call it as sample().

Returns

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

(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.