Skip to content

chunk

The chunk() function splits an iterable (generator or array) into fixed-size blocks and returns a generator of arrays. The last block can be shorter if the size does not evenly divide the input.

Interactive example

Syntax

Classic signature

typescript
function chunk<
	const GenericElement extends unknown
>(
  input: Iterable<GenericElement>,
  size: number,
): Generator<GenericElement[], unknown, unknown>

Curried signature

typescript
function chunk<
	const GenericElement extends unknown
>(
  size: number,
): (input: Iterable<GenericElement>) => Generator<GenericElement[], unknown, unknown>

Parameters

  • input: Iterable (generator or array) to split.
  • size: Size of each block.

Return value

A generator producing arrays containing the input blocks. The input is consumed lazily.

See also

  • filter - Filters the elements of a generator
  • map - Transforms the elements of a generator
  • reduce - Reduces a generator to a value

Released under the MIT license.