<B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (head: A, tail: Array<A>) => C
}): (self: ReadonlyArray<A>) => B | C
<A, B, C = B>(
self: ReadonlyArray<A>,
options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (head: A, tail: Array<A>) => C
}
): B | CPattern-matches on an array from the left, providing the first element and the remaining elements separately.
When to use
Use when you need to branch on an array and handle the non-empty case as the first element plus the remaining elements.
Details
onNonEmpty receives (head, tail) where tail is the rest of the array.
Example (Destructuring head and tail)
import { Array } from "effect"
const matchLeft = Array.matchLeft({
onEmpty: () => "empty",
onNonEmpty: (head, tail) => `head: ${head}, tail: ${tail.length}`
})
console.log(matchLeft([])) // "empty"
console.log(matchLeft([1, 2, 3])) // "head: 1, tail: 2"export const const matchLeft: {
<B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (
head: A,
tail: Array<A>
) => C
}): (self: ReadonlyArray<A>) => B | C
<A, B, C = B>(
self: ReadonlyArray<A>,
options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (
head: A,
tail: Array<A>
) => C
}
): B | C
}
Pattern-matches on an array from the left, providing the first element and
the remaining elements separately.
When to use
Use when you need to branch on an array and handle the non-empty case as the
first element plus the remaining elements.
Details
onNonEmpty receives (head, tail) where tail is the rest of the array.
Example (Destructuring head and tail)
import { Array } from "effect"
const matchLeft = Array.matchLeft({
onEmpty: () => "empty",
onNonEmpty: (head, tail) => `head: ${head}, tail: ${tail.length}`
})
console.log(matchLeft([])) // "empty"
console.log(matchLeft([1, 2, 3])) // "head: 1, tail: 2"
matchLeft: {
<function (type parameter) B in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
B, function (type parameter) A in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
A, function (type parameter) C in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
C = function (type parameter) B in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
B>(
options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (
head: A,
tail: Array<A>
) => C
}
options: {
readonly onEmpty: LazyArg<B>onEmpty: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) B in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
B>
readonly onNonEmpty: (head: A, tail: Array<A>) => ConNonEmpty: (head: Ahead: function (type parameter) A in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
A, tail: A[]tail: interface Array<T>Array<function (type parameter) A in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
A>) => function (type parameter) C in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
C
}
): (self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
A>) => function (type parameter) B in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
B | function (type parameter) C in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): (self: ReadonlyArray<A>) => B | C
C
<function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
A, function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
B, function (type parameter) C in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
C = function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
B>(
self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
A>,
options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (
head: A,
tail: Array<A>
) => C
}
options: {
readonly onEmpty: LazyArg<B>onEmpty: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
B>
readonly onNonEmpty: (head: A, tail: Array<A>) => ConNonEmpty: (head: Ahead: function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
A, tail: A[]tail: interface Array<T>Array<function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
A>) => function (type parameter) C in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
C
}
): function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
B | function (type parameter) C in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
C
} = dual<(...args: Array<any>) => any, <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}) => B | C>(arity: 2, body: <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}) => B | C): ((...args: Array<any>) => any) & (<A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}) => B | C) (+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, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
A, function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
B, function (type parameter) C in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
C = function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
B>(
self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
A>,
{ onEmpty: LazyArg<B>onEmpty, onNonEmpty: (head: A, tail: Array<A>) => ConNonEmpty }: {
readonly onEmpty: LazyArg<B>onEmpty: type LazyArg<A> = () => AA zero-argument function that produces a value when invoked.
When to use
Use to type a lazy value provider that should not run until called.
Example (Creating a lazy argument)
import { Function } from "effect"
const constNull: Function.LazyArg<null> = Function.constant(null)
LazyArg<function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
B>
readonly onNonEmpty: (head: A, tail: Array<A>) => ConNonEmpty: (head: Ahead: function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
A, tail: A[]tail: interface Array<T>Array<function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
A>) => function (type parameter) C in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
C
}
): function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
B | function (type parameter) C in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (head: A, tail: Array<A>) => C;
}): B | C
C => 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(self: readonly A[]self) ? onNonEmpty: (head: A, tail: Array<A>) => ConNonEmpty(const headNonEmpty: <A>(
self: NonEmptyReadonlyArray<A>
) => A
Returns the first element of a NonEmptyReadonlyArray directly (no Option
wrapper).
When to use
Use to get the first element without Option wrapping when the array is known
to be non-empty.
Example (Getting the head of a non-empty array)
import { Array } from "effect"
console.log(Array.headNonEmpty([1, 2, 3, 4])) // 1
headNonEmpty(self: readonly [A, ...A[]](parameter) self: {
0: A;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<A>>): Array<A>; (...items: Array<A | ConcatArray<A>>): Array<A> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<A>;
indexOf: (searchElement: A, fromIndex?: number) => number;
lastIndexOf: (searchElement: A, fromIndex?: number) => number;
every: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: A, index: number, array: ReadonlyArray<A>) => void, thisArg?: any) => void;
map: (callbackfn: (value: A, index: number, array: ReadonlyArray<A>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): Array<S>; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): Array<A> };
reduce: { (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A): A; (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A, initialValue: A): A; (callbac…;
reduceRight: { (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A): A; (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A, initialValue: A): A; (callbac…;
find: { (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => unknown, thisArg?: any): A | undefined };
findIndex: (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, A]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<A>;
includes: (searchElement: A, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: A, index: number, array: Array<A>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => A | undefined;
findLast: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): A | undefined };
findLastIndex: (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any) => number;
toReversed: () => Array<A>;
toSorted: (compareFn?: ((a: A, b: A) => number) | undefined) => Array<A>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<A>): Array<A>; (start: number, deleteCount?: number): Array<A> };
with: (index: number, value: A) => Array<A>;
}
self), const tailNonEmpty: <A>(
self: NonEmptyReadonlyArray<A>
) => Array<A>
Returns all elements except the first of a NonEmptyReadonlyArray.
When to use
Use to get all elements after the first when the array is known to be non-empty.
Example (Getting the tail of a non-empty array)
import { Array } from "effect"
console.log(Array.tailNonEmpty([1, 2, 3, 4])) // [2, 3, 4]
tailNonEmpty(self: readonly [A, ...A[]](parameter) self: {
0: A;
length: number;
toString: () => string;
toLocaleString: { (): string; (locales: string | string[], options?: Intl.NumberFormatOptions & Intl.DateTimeFormatOptions): string };
concat: { (...items: Array<ConcatArray<A>>): Array<A>; (...items: Array<A | ConcatArray<A>>): Array<A> };
join: (separator?: string) => string;
slice: (start?: number, end?: number) => Array<A>;
indexOf: (searchElement: A, fromIndex?: number) => number;
lastIndexOf: (searchElement: A, fromIndex?: number) => number;
every: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): this is readonly S[]; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): boolean };
some: (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any) => boolean;
forEach: (callbackfn: (value: A, index: number, array: ReadonlyArray<A>) => void, thisArg?: any) => void;
map: (callbackfn: (value: A, index: number, array: ReadonlyArray<A>) => U, thisArg?: any) => Array<U>;
filter: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): Array<S>; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): Array<A> };
reduce: { (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A): A; (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A, initialValue: A): A; (callbac…;
reduceRight: { (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A): A; (callbackfn: (previousValue: A, currentValue: A, currentIndex: number, array: ReadonlyArray<A>) => A, initialValue: A): A; (callbac…;
find: { (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => unknown, thisArg?: any): A | undefined };
findIndex: (predicate: (value: A, index: number, obj: ReadonlyArray<A>) => unknown, thisArg?: any) => number;
entries: () => ArrayIterator<[number, A]>;
keys: () => ArrayIterator<number>;
values: () => ArrayIterator<A>;
includes: (searchElement: A, fromIndex?: number) => boolean;
flatMap: (callback: (this: This, value: A, index: number, array: Array<A>) => U | ReadonlyArray<U>, thisArg?: This | undefined) => Array<U>;
flat: (this: A, depth?: D | undefined) => Array<FlatArray<A, D>>;
at: (index: number) => A | undefined;
findLast: { (predicate: (value: A, index: number, array: ReadonlyArray<A>) => value is S, thisArg?: any): S | undefined; (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any): A | undefined };
findLastIndex: (predicate: (value: A, index: number, array: ReadonlyArray<A>) => unknown, thisArg?: any) => number;
toReversed: () => Array<A>;
toSorted: (compareFn?: ((a: A, b: A) => number) | undefined) => Array<A>;
toSpliced: { (start: number, deleteCount: number, ...items: Array<A>): Array<A>; (start: number, deleteCount?: number): Array<A> };
with: (index: number, value: A) => Array<A>;
}
self)) : onEmpty: LazyArg<B>onEmpty())