Skip to content

chunk

chunk<T>(xs, size): Generator<T[], void>

Creates a generator that splits the given Iterable into chunks of the required size. If no even chunks can be created, the last chunk will have fewer elements.

Example

collect(chunk([1, 2, 3, 4, 5], 1))
// => [[1], [2], [3], [4], [5]]
collect(chunk([1, 2, 3, 4, 5], 3))
// => [[1, 2, 3], [4, 5]]
collect(chunk([1, 2, 3], 0))
// => [[1], [2], [3]]

Alternatives

Type Parameters

T

The element type.

Parameters

xs

Iterable<T>

The values to split in chunks.

size

number

The maximum size of a chunk, constrained to minimum value of 1.

Returns

Generator<T[], void>

A chunk generator.