(n: number): <A>(self: Iterable<A>) => Iterable<Array<A>>
<A>(self: Iterable<A>, n: number): Iterable<Array<A>>Splits an Iterable into length-n pieces. The last piece will be shorter if n does not evenly divide the length of
the Iterable.
Example (Chunking an iterable)
import { Iterable } from "effect"
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const chunks = Iterable.chunksOf(numbers, 3)
console.log(Array.from(chunks).map((chunk) => Array.from(chunk)))
// [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// Last chunk can be shorter
const uneven = [1, 2, 3, 4, 5, 6, 7]
const chunks2 = Iterable.chunksOf(uneven, 3)
console.log(Array.from(chunks2).map((chunk) => Array.from(chunk)))
// [[1, 2, 3], [4, 5, 6], [7]]
// Chunk size larger than iterable
const small = [1, 2]
const chunks3 = Iterable.chunksOf(small, 5)
console.log(Array.from(chunks3).map((chunk) => Array.from(chunk)))
// [[1, 2]]
// Process data in batches
const data = Iterable.range(1, 100)
const batches = Iterable.chunksOf(data, 10)
const batchSums = Iterable.map(
batches,
(batch) => Iterable.reduce(batch, 0, (sum, n) => sum + n)
)
console.log(Array.from(Iterable.take(batchSums, 3))) // [55, 155, 255]export const const chunksOf: {
(n: number): <A>(
self: Iterable<A>
) => Iterable<Array<A>>
<A>(self: Iterable<A>, n: number): Iterable<
Array<A>
>
}
Splits an Iterable into length-n pieces. The last piece will be shorter if n does not evenly divide the length of
the Iterable.
Example (Chunking an iterable)
import { Iterable } from "effect"
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const chunks = Iterable.chunksOf(numbers, 3)
console.log(Array.from(chunks).map((chunk) => Array.from(chunk)))
// [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// Last chunk can be shorter
const uneven = [1, 2, 3, 4, 5, 6, 7]
const chunks2 = Iterable.chunksOf(uneven, 3)
console.log(Array.from(chunks2).map((chunk) => Array.from(chunk)))
// [[1, 2, 3], [4, 5, 6], [7]]
// Chunk size larger than iterable
const small = [1, 2]
const chunks3 = Iterable.chunksOf(small, 5)
console.log(Array.from(chunks3).map((chunk) => Array.from(chunk)))
// [[1, 2]]
// Process data in batches
const data = Iterable.range(1, 100)
const batches = Iterable.chunksOf(data, 10)
const batchSums = Iterable.map(
batches,
(batch) => Iterable.reduce(batch, 0, (sum, n) => sum + n)
)
console.log(Array.from(Iterable.take(batchSums, 3))) // [55, 155, 255]
chunksOf: {
(n: numbern: number): <function (type parameter) A in <A>(self: Iterable<A>): Iterable<Array<A>>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): Iterable<Array<A>>A>) => interface Iterable<T, TReturn = any, TNext = any>Iterable<interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>): Iterable<Array<A>>A>>
<function (type parameter) A in <A>(self: Iterable<A>, n: number): Iterable<Array<A>>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, n: number): Iterable<Array<A>>A>, n: numbern: number): interface Iterable<T, TReturn = any, TNext = any>Iterable<interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): Iterable<Array<A>>A>>
} = import dualdual(2, <function (type parameter) A in <A>(self: Iterable<A>, n: number): Iterable<Array<A>>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, n: number): Iterable<Array<A>>A>, n: numbern: number): interface Iterable<T, TReturn = any, TNext = any>Iterable<interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): Iterable<Array<A>>A>> => {
const const safeN: numbersafeN = var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.max(...values: number[]): numberReturns the larger of a set of supplied numeric expressions.
max(1, var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.floor(x: number): numberReturns the greatest integer less than or equal to its numeric argument.
floor(n: numbern))
return ({
[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]() {
let let iterator:
| Iterator<A, any, any>
| undefined
iterator: interface Iterator<T, TReturn = any, TNext = any>Iterator<function (type parameter) A in <A>(self: Iterable<A>, n: number): Iterable<Array<A>>A> | undefined = self: Iterable<A>self[var Symbol: SymbolConstructorSymbol.SymbolConstructor.iterator: typeof Symbol.iteratorA method that returns the default iterator for an object. Called by the semantics of the
for-of statement.
iterator]()
return {
Iterator<A[], any, any>.next(...[value]: [] | [any]): IteratorResult<A[], any>next() {
if (let iterator:
| Iterator<A, any, any>
| undefined
iterator === var undefinedundefined) {
return { IteratorReturnResult<TReturn>.done: truedone: true, IteratorReturnResult<any>.value: anyvalue: var undefinedundefined }
}
const const chunk: A[]chunk: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): Iterable<Array<A>>A> = []
for (let let i: numberi = 0; let i: numberi < const safeN: numbersafeN; let i: numberi++) {
const const result: IteratorResult<A, any>result = let iterator: Iterator<A, any, any>iterator.Iterator<A, any, any>.next(...[value]: [] | [any]): IteratorResult<A, any>next()
if (const result: IteratorResult<A, any>result.done?: boolean | undefineddone) {
let iterator:
| Iterator<A, any, any>
| undefined
iterator = var undefinedundefined
return const chunk: A[]chunk.Array<A>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length === 0 ? { IteratorReturnResult<TReturn>.done: truedone: true, IteratorReturnResult<any>.value: anyvalue: var undefinedundefined } : { IteratorYieldResult<TYield>.done?: false | undefineddone: false, IteratorYieldResult<A[]>.value: A[]value: const chunk: A[]chunk }
}
const chunk: A[]chunk.Array<A>.push(...items: A[]): numberAppends new elements to the end of an array, and returns the new length of the array.
push(const result: IteratorYieldResult<A>result.IteratorYieldResult<A>.value: Avalue)
}
return { IteratorYieldResult<TYield>.done?: false | undefineddone: false, IteratorYieldResult<A[]>.value: A[]value: const chunk: A[]chunk }
}
}
}
})
})