Hyperlinkv0.8.0-beta.28

Array

Array.chunksOfconsteffect/Array.ts:2887
(n: number): <S extends Iterable<any>>(
  self: S
) => ReadonlyArray.With<S, NonEmptyArray<ReadonlyArray.Infer<S>>>
<A>(self: NonEmptyReadonlyArray<A>, n: number): NonEmptyArray<
  NonEmptyArray<A>
>
<A>(self: Iterable<A>, n: number): Array<NonEmptyArray<A>>

Splits an iterable into chunks of length n. The last chunk may be shorter if n does not evenly divide the length.

When to use

Use to divide an iterable into a new array of non-overlapping chunks with a maximum chunk size.

Details

chunksOf(n)([]) is [], not [[]]. Each chunk is a NonEmptyArray, and the outer return type preserves NonEmptyArray.

Example (Chunking an array)

import { Array } from "effect"

console.log(Array.chunksOf([1, 2, 3, 4, 5], 2)) // [[1, 2], [3, 4], [5]]
splittingsplitwindow
Source effect/Array.ts:288715 lines
export const chunksOf: {
  (
    n: number
  ): <S extends Iterable<any>>(
    self: S
  ) => ReadonlyArray.With<S, NonEmptyArray<ReadonlyArray.Infer<S>>>
  <A>(self: NonEmptyReadonlyArray<A>, n: number): NonEmptyArray<NonEmptyArray<A>>
  <A>(self: Iterable<A>, n: number): Array<NonEmptyArray<A>>
} = dual(2, <A>(self: Iterable<A>, n: number): Array<NonEmptyArray<A>> => {
  const input = fromIterable(self)
  if (isReadonlyArrayNonEmpty(input)) {
    return chop(input, splitAtNonEmpty(n))
  }
  return []
})
Referenced by 1 symbols