<
const T extends ReadonlyArray<unknown>,
const I extends ReadonlyArray<Indices<T>>
>(
indices: I
): (self: T) => PickTuple<T, I[number]>
<
const T extends ReadonlyArray<unknown>,
const I extends ReadonlyArray<Indices<T>>
>(
self: T,
indices: I
): PickTuple<T, I[number]>Creates a new tuple containing only the elements at the specified indices.
When to use
Use to select a subset of elements from a tuple by position.
Details
The result order matches the order of the provided indices.
Example (Selecting elements by index)
import { Tuple } from "effect"
const result = Tuple.pick(["a", "b", "c", "d"], [0, 2, 3])
console.log(result) // ["a", "c", "d"]export const const pick: {
<
const T extends ReadonlyArray<unknown>,
const I extends ReadonlyArray<Indices<T>>
>(
indices: I
): (self: T) => PickTuple<T, I[number]>
<
const T extends ReadonlyArray<unknown>,
const I extends ReadonlyArray<Indices<T>>
>(
self: T,
indices: I
): PickTuple<T, I[number]>
}
Creates a new tuple containing only the elements at the specified indices.
When to use
Use to select a subset of elements from a tuple by position.
Details
The result order matches the order of the provided indices.
Example (Selecting elements by index)
import { Tuple } from "effect"
const result = Tuple.pick(["a", "b", "c", "d"], [0, 2, 3])
console.log(result) // ["a", "c", "d"]
pick: {
<const function (type parameter) T in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(indices: I): (self: T) => PickTuple<T, I[number]>T extends interface ReadonlyArray<T>ReadonlyArray<unknown>, const function (type parameter) I in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(indices: I): (self: T) => PickTuple<T, I[number]>I extends interface ReadonlyArray<T>ReadonlyArray<type Indices<
T extends ReadonlyArray<unknown>
> = Partial<T>["length"] extends T["length"]
? never
: Partial<T>["length"]
Indices<function (type parameter) T in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(indices: I): (self: T) => PickTuple<T, I[number]>T>>>(
indices: const I extends ReadonlyArray<Indices<T>>indices: function (type parameter) I in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(indices: I): (self: T) => PickTuple<T, I[number]>I
): (self: const T extends ReadonlyArray<unknown>self: function (type parameter) T in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(indices: I): (self: T) => PickTuple<T, I[number]>T) => type PickTuple<
T extends ReadonlyArray<unknown>,
K
> = 0 extends T["length"]
? []
: _BuildTuple<
T,
K,
0 extends K ? [T[K & 0]] : [],
[unknown]
>
PickTuple<function (type parameter) T in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(indices: I): (self: T) => PickTuple<T, I[number]>T, function (type parameter) I in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(indices: I): (self: T) => PickTuple<T, I[number]>I[number]>
<const function (type parameter) T in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(self: T, indices: I): PickTuple<T, I[number]>T extends interface ReadonlyArray<T>ReadonlyArray<unknown>, const function (type parameter) I in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(self: T, indices: I): PickTuple<T, I[number]>I extends interface ReadonlyArray<T>ReadonlyArray<type Indices<
T extends ReadonlyArray<unknown>
> = Partial<T>["length"] extends T["length"]
? never
: Partial<T>["length"]
Indices<function (type parameter) T in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(self: T, indices: I): PickTuple<T, I[number]>T>>>(
self: const T extends ReadonlyArray<unknown>self: function (type parameter) T in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(self: T, indices: I): PickTuple<T, I[number]>T,
indices: const I extends ReadonlyArray<Indices<T>>indices: function (type parameter) I in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(self: T, indices: I): PickTuple<T, I[number]>I
): type PickTuple<
T extends ReadonlyArray<unknown>,
K
> = 0 extends T["length"]
? []
: _BuildTuple<
T,
K,
0 extends K ? [T[K & 0]] : [],
[unknown]
>
PickTuple<function (type parameter) T in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(self: T, indices: I): PickTuple<T, I[number]>T, function (type parameter) I in <const T extends ReadonlyArray<unknown>, const I extends ReadonlyArray<Indices<T>>>(self: T, indices: I): PickTuple<T, I[number]>I[number]>
} = dual<(...args: Array<any>) => any, <const T extends ReadonlyArray<unknown>>(self: T, indices: ReadonlyArray<number>) => unknown[]>(arity: 2, body: <const T extends ReadonlyArray<unknown>>(self: T, indices: ReadonlyArray<number>) => unknown[]): ((...args: Array<any>) => any) & (<const T extends ReadonlyArray<unknown>>(self: T, indices: ReadonlyArray<number>) => unknown[]) (+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,
<const function (type parameter) T in <const T extends ReadonlyArray<unknown>>(self: T, indices: ReadonlyArray<number>): unknown[]T extends interface ReadonlyArray<T>ReadonlyArray<unknown>>(
self: const T extends ReadonlyArray<unknown>self: function (type parameter) T in <const T extends ReadonlyArray<unknown>>(self: T, indices: ReadonlyArray<number>): unknown[]T,
indices: readonly number[]indices: interface ReadonlyArray<T>ReadonlyArray<number>
) => {
return indices: readonly number[]indices.ReadonlyArray<number>.map<unknown>(callbackfn: (value: number, index: number, array: readonly number[]) => unknown, thisArg?: any): unknown[]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map((i: numberi) => self: const T extends ReadonlyArray<unknown>self[i: numberi])
}
)