Hyperlinkv0.8.0-beta.28

Chunk

Chunk.splitconsteffect/Chunk.ts:2036
(n: number): <A>(self: Chunk<A>) => Chunk<Chunk<A>>
<A>(self: Chunk<A>, n: number): Chunk<Chunk<A>>

Splits a chunk into up to n chunks, distributing elements in order.

Details

The chunk size is derived from the input length and n; the final chunk may contain fewer elements than the others.

Example (Splitting chunks into groups)

import { Chunk } from "effect"

const chunk = Chunk.make(1, 2, 3, 4, 5, 6, 7, 8, 9)
const chunks = Chunk.split(chunk, 3)
console.log(Chunk.toArray(chunks).map(Chunk.toArray))
// [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

// Uneven split
const chunk2 = Chunk.make(1, 2, 3, 4, 5, 6, 7, 8)
const chunks2 = Chunk.split(chunk2, 3)
console.log(Chunk.toArray(chunks2).map(Chunk.toArray))
// [[1, 2, 3], [4, 5, 6], [7, 8]]

// Split into 1 chunk
const chunks3 = Chunk.split(chunk, 1)
console.log(Chunk.toArray(chunks3).map(Chunk.toArray))
// [[1, 2, 3, 4, 5, 6, 7, 8, 9]]
splitting
Source effect/Chunk.ts:20364 lines
export const split: {
  (n: number): <A>(self: Chunk<A>) => Chunk<Chunk<A>>
  <A>(self: Chunk<A>, n: number): Chunk<Chunk<A>>
} = dual(2, <A>(self: Chunk<A>, n: number) => chunksOf(self, Math.ceil(self.length / Math.floor(n))))