(n: number): <A>(
self: Iterable<A>
) => [beforeIndex: Array<A>, fromIndex: Array<A>]
<A>(self: Iterable<A>, n: number): [
beforeIndex: Array<A>,
fromIndex: Array<A>
]Splits an iterable into two arrays at the given index.
When to use
Use to divide an array into a prefix and suffix at a specific position.
Details
n can be 0, in which case all elements are placed in the second array.
The index is floored to an integer.
Example (Splitting at an index)
import { Array } from "effect"
console.log(Array.splitAt([1, 2, 3, 4, 5], 3)) // [[1, 2, 3], [4, 5]]export const const splitAt: {
(n: number): <A>(
self: Iterable<A>
) => [
beforeIndex: Array<A>,
fromIndex: Array<A>
]
<A>(self: Iterable<A>, n: number): [
beforeIndex: Array<A>,
fromIndex: Array<A>
]
}
Splits an iterable into two arrays at the given index.
When to use
Use to divide an array into a prefix and suffix at a specific position.
Details
n can be 0, in which case all elements are placed in the second array.
The index is floored to an integer.
Example (Splitting at an index)
import { Array } from "effect"
console.log(Array.splitAt([1, 2, 3, 4, 5], 3)) // [[1, 2, 3], [4, 5]]
splitAt: {
(n: numbern: number): <function (type parameter) A in <A>(self: Iterable<A>): [beforeIndex: Array<A>, fromIndex: Array<A>]A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): [beforeIndex: Array<A>, fromIndex: Array<A>]A>) => [A[]beforeIndex: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>): [beforeIndex: Array<A>, fromIndex: Array<A>]A>, A[]fromIndex: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>): [beforeIndex: Array<A>, fromIndex: Array<A>]A>]
<function (type parameter) A in <A>(self: Iterable<A>, n: number): [beforeIndex: Array<A>, fromIndex: 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): [beforeIndex: Array<A>, fromIndex: Array<A>]A>, n: numbern: number): [A[]beforeIndex: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): [beforeIndex: Array<A>, fromIndex: Array<A>]A>, A[]fromIndex: interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): [beforeIndex: Array<A>, fromIndex: Array<A>]A>]
} = dual<(...args: Array<any>) => any, <A>(self: Iterable<A>, n: number) => [Array<A>, Array<A>]>(arity: 2, body: <A>(self: Iterable<A>, n: number) => [Array<A>, Array<A>]): ((...args: Array<any>) => any) & (<A>(self: Iterable<A>, n: number) => [Array<A>, Array<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): [Array<A>, 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<A>, Array<A>]A>, n: numbern: number): [interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): [Array<A>, Array<A>]A>, interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, n: number): [Array<A>, Array<A>]A>] => {
const const input: A[]input = const Array: ArrayConstructorExposes the global array constructor.
When to use
Use to access native JavaScript array constructor methods such as isArray
or from from the Effect module namespace.
Example (Accessing the Array constructor)
import { Array } from "effect"
const arr = new Array.Array(3)
console.log(arr) // [undefined, undefined, undefined]
Array.ArrayConstructor.from<A>(iterable: Iterable<A> | ArrayLike<A>): A[] (+3 overloads)Creates an array from an iterable object.
from(self: Iterable<A>self)
const const _n: number_n = 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)
if (const isReadonlyArrayNonEmpty: <A>(
self: ReadonlyArray<A>
) => self is NonEmptyReadonlyArray<A>
Checks whether a ReadonlyArray is non-empty, narrowing the type to
NonEmptyReadonlyArray.
When to use
Use when you need to prove a readonly array has at least one element without
requiring mutable array methods afterward.
Example (Checking for a non-empty readonly array)
import { Array } from "effect"
console.log(Array.isReadonlyArrayNonEmpty([])) // false
console.log(Array.isReadonlyArrayNonEmpty([1, 2, 3])) // true
isReadonlyArrayNonEmpty(const input: A[]input)) {
if (const _n: number_n >= 1) {
return const splitAtNonEmpty: {
(n: number): <A>(
self: NonEmptyReadonlyArray<A>
) => [
beforeIndex: NonEmptyArray<A>,
fromIndex: Array<A>
]
<A>(
self: NonEmptyReadonlyArray<A>,
n: number
): [
beforeIndex: NonEmptyArray<A>,
fromIndex: Array<A>
]
}
splitAtNonEmpty(const input: A[] & readonly [A, ...A[]]input, const _n: number_n)
}
return [[], const input: A[] & readonly [A, ...A[]]input]
}
return [const input: A[]input, []]
})