chunk() function
요소들을 size 길이의 그룹으로 나눈 Iterable/AsyncIterable을 반환합니다. iterableIterator가 균등하게 나눠지지 않으면, 마지막 chunk는 나머지 요소들이 됩니다.
Signature:
typescript
declare function chunk<T>(
size: number,
iterable: Iterable<T>,
): IterableIterator<T[]>;Example
ts
const iter = chunk(2, [1, 2, 3, 4]);
iter.next(); // {done:false, value:[1, 2]}
iter.next(); // {done:false, value:[3, 4]}
iter.next(); // {done:true, value: undefined}
// with pipe
pipe([1, 2, 3, 4], chunk(2), toArray); // [[1, 2],[3, 4]]
await pipe(Promise.resolve([1, 2, 3, 4]), chunk(2), toArray); // [[1, 2],[3, 4]]
// with toAsync
await pipe(
[
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
Promise.resolve(4),
],
toAsync,
chunk(2),
toArray,
); // [[1, 2],[3, 4]]