<B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (init: Array<A>, last: A) => C
}): (self: ReadonlyArray<A>) => B | C
<A, B, C = B>(
self: ReadonlyArray<A>,
options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (init: Array<A>, last: A) => C
}
): B | CPattern-matches on an array from the right, providing all elements except the last and the last element separately.
When to use
Use when you need to branch on an array and handle the non-empty case as the elements before the last plus the last element.
Details
onNonEmpty receives (init, last) where init is everything but the last element.
Example (Destructuring init and last)
import { Array } from "effect"
const matchRight = Array.matchRight({
onEmpty: () => "empty",
onNonEmpty: (init, last) => `init: ${init.length}, last: ${last}`
})
console.log(matchRight([])) // "empty"
console.log(matchRight([1, 2, 3])) // "init: 2, last: 3"export const const matchRight: {
<B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (
init: Array<A>,
last: A
) => C
}): (self: ReadonlyArray<A>) => B | C
<A, B, C = B>(
self: ReadonlyArray<A>,
options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (
init: Array<A>,
last: A
) => C
}
): B | C
}
Pattern-matches on an array from the right, providing all elements except the
last and the last element separately.
When to use
Use when you need to branch on an array and handle the non-empty case as the
elements before the last plus the last element.
Details
onNonEmpty receives (init, last) where init is everything but the last element.
Example (Destructuring init and last)
import { Array } from "effect"
const matchRight = Array.matchRight({
onEmpty: () => "empty",
onNonEmpty: (init, last) => `init: ${init.length}, last: ${last}`
})
console.log(matchRight([])) // "empty"
console.log(matchRight([1, 2, 3])) // "init: 2, last: 3"
matchRight: {
<function (type parameter) B in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): (self: ReadonlyArray<A>) => B | C
B, function (type parameter) A in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): (self: ReadonlyArray<A>) => B | C
A, function (type parameter) C in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): (self: ReadonlyArray<A>) => B | C
C = function (type parameter) B in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): (self: ReadonlyArray<A>) => B | C
B>(
options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (
init: Array<A>,
last: 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: (init: Array<A>, last: A) => C;
}): (self: ReadonlyArray<A>) => B | C
B>
readonly onNonEmpty: (init: Array<A>, last: A) => ConNonEmpty: (init: A[]init: interface Array<T>Array<function (type parameter) A in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): (self: ReadonlyArray<A>) => B | C
A>, last: Alast: function (type parameter) A in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): (self: ReadonlyArray<A>) => B | C
A) => function (type parameter) C in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: 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: (init: Array<A>, last: A) => C;
}): (self: ReadonlyArray<A>) => B | C
A>) => function (type parameter) B in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): (self: ReadonlyArray<A>) => B | C
B | function (type parameter) C in <B, A, C = B>(options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: 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: (init: Array<A>, last: A) => C;
}): B | C
A, function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): B | C
B, function (type parameter) C in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): B | C
C = function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: 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: (init: Array<A>, last: A) => C;
}): B | C
A>,
options: {
readonly onEmpty: LazyArg<B>
readonly onNonEmpty: (
init: Array<A>,
last: 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: (init: Array<A>, last: A) => C;
}): B | C
B>
readonly onNonEmpty: (init: Array<A>, last: A) => ConNonEmpty: (init: A[]init: interface Array<T>Array<function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): B | C
A>, last: Alast: function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): B | C
A) => function (type parameter) C in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): B | C
C
}
): function (type parameter) B in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): B | C
B | function (type parameter) C in <A, B, C = B>(self: ReadonlyArray<A>, options: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: 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: (init: Array<A>, last: A) => C;
}) => B | C>(arity: 2, body: <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}) => B | C): ((...args: Array<any>) => any) & (<A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: 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: (init: Array<A>, last: 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: (init: Array<A>, last: 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: (init: Array<A>, last: 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: (init: Array<A>, last: 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: (init: Array<A>, last: A) => C;
}): B | C
A>,
{ onEmpty: LazyArg<B>onEmpty, onNonEmpty: (init: Array<A>, last: 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: (init: Array<A>, last: A) => C;
}): B | C
B>
readonly onNonEmpty: (init: Array<A>, last: A) => ConNonEmpty: (init: A[]init: interface Array<T>Array<function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: A) => C;
}): B | C
A>, last: Alast: function (type parameter) A in <A, B, C = B>(self: ReadonlyArray<A>, { onEmpty, onNonEmpty }: {
readonly onEmpty: LazyArg<B>;
readonly onNonEmpty: (init: Array<A>, last: 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: (init: Array<A>, last: 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: (init: Array<A>, last: 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: (init: Array<A>, last: 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: (init: Array<A>, last: A) => ConNonEmpty(const initNonEmpty: <A>(
self: NonEmptyReadonlyArray<A>
) => Array<A>
Returns all elements except the last of a NonEmptyReadonlyArray.
When to use
Use to get all elements before the last when the array is known to be non-empty.
Example (Getting init of a non-empty array)
import { Array } from "effect"
console.log(Array.initNonEmpty([1, 2, 3, 4])) // [1, 2, 3]
initNonEmpty(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 lastNonEmpty: <A>(
self: NonEmptyReadonlyArray<A>
) => A
Returns the last element of a NonEmptyReadonlyArray directly (no Option
wrapper).
When to use
Use to get the last element without Option wrapping when the array is known
to be non-empty.
Example (Getting the last of a non-empty array)
import { Array } from "effect"
console.log(Array.lastNonEmpty([1, 2, 3, 4])) // 4
lastNonEmpty(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())