(n: number): <A>(self: Iterable<A>) => Array<Array<A>>
<A>(self: Iterable<A>, n: number): Array<Array<A>>Splits an iterable into n roughly equal-sized chunks.
When to use
Use to distribute elements across a fixed number of groups, such as when splitting work across threads.
Details
Uses chunksOf(ceil(length / n)) internally. The last chunk may be shorter.
Example (Splitting into groups)
import { Array } from "effect"
console.log(Array.split([1, 2, 3, 4, 5, 6, 7, 8], 3)) // [[1, 2, 3], [4, 5, 6], [7, 8]]export const const split: {
(n: number): <A>(
self: Iterable<A>
) => Array<Array<A>>
<A>(self: Iterable<A>, n: number): Array<
Array<A>
>
}
Splits an iterable into n roughly equal-sized chunks.
When to use
Use to distribute elements across a fixed number of groups, such as when splitting work across threads.
Details
Uses chunksOf(ceil(length / n)) internally. The last chunk may be shorter.
Example (Splitting into groups)
import { Array } from "effect"
console.log(Array.split([1, 2, 3, 4, 5, 6, 7, 8], 3)) // [[1, 2, 3], [4, 5, 6], [7, 8]]
split: {
(n: numbern: number): <function (type parameter) A in <A>(self: Iterable<A>): Array<Array<A>>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): Array<Array<A>>A>) => interface Array<T>Array<interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>): Array<Array<A>>A>>
<function (type parameter) A in <A>(self: Iterable<A>, n: number): Array<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): Array<Array<A>>A>, n: numbern: number): interface Array<T>Array<interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): Array<Array<A>>A>>
} = dual<(...args: Array<any>) => any, <A>(self: Iterable<A>, n: number) => [A, ...A[]][]>(arity: 2, body: <A>(self: Iterable<A>, n: number) => [A, ...A[]][]): ((...args: Array<any>) => any) & (<A>(self: Iterable<A>, n: number) => [A, ...A[]][]) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(2, <function (type parameter) A in <A>(self: Iterable<A>, n: number): [A, ...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): [A, ...A[]][]A>, n: numbern: number) => {
const const input: A[]input = const fromIterable: <A>(
collection: Iterable<A>
) => A[]
Converts an Iterable to an Array.
When to use
Use to convert any Iterable (Set, Generator, etc.) into an array.
Details
If the input is already an array, this returns it by reference without
copying. Otherwise, it creates a new array from the iterable. Use copy if
you need a fresh array even when the input is already an array.
Example (Converting a Set to an array)
import { Array } from "effect"
const result = Array.fromIterable(new Set([1, 2, 3]))
console.log(result) // [1, 2, 3]
fromIterable(self: Iterable<A>self)
return 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>
>
}
chunksOf(const input: A[]input, var Math: MathAn intrinsic object that provides basic mathematics functionality and constants.
Math.Math.ceil(x: number): numberReturns the smallest integer greater than or equal to its numeric argument.
ceil(const input: A[]input.Array<T>.length: numberGets or sets the length of the array. This is a number one higher than the highest index in the array.
length / 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)))
})