Tuple overview
This module provides utility functions for working with tuples in TypeScript.
Added in v2.0.0
Table of contents
combinators
getEquivalence
Given a tuple of Equivalence
s returns a new Equivalence
that compares values of a tuple by applying each Equivalence
to the corresponding element of the tuple.
Signature
export declare const getEquivalence: <T extends ReadonlyArray<Equivalence.Equivalence<any>>>(
...isEquivalents: T
) => Equivalence.Equivalence<
Readonly<{ [I in keyof T]: [T[I]] extends [Equivalence.Equivalence<infer A>] ? A : never }>
>
Added in v2.0.0
getOrder
This function creates and returns a new Order
for a tuple of values based on the given Order
s for each element in the tuple. The returned Order
compares two tuples of the same type by applying the corresponding Order
to each element in the tuple. It is useful when you need to compare two tuples of the same type and you have a specific way of comparing each element of the tuple.
Signature
export declare const getOrder: <T extends ReadonlyArray<order.Order<any>>>(
...elements: T
) => order.Order<{ [I in keyof T]: [T[I]] extends [order.Order<infer A>] ? A : never }>
Added in v2.0.0
concatenating
appendElement
Appends an element to the end of a tuple.
Signature
export declare const appendElement: {
<B>(that: B): <A extends ReadonlyArray<unknown>>(self: A) => [...A, B]
<A extends ReadonlyArray<unknown>, B>(self: A, that: B): [...A, B]
}
Added in v2.0.0
constructors
make
Constructs a new tuple from the provided values.
Signature
export declare const make: <A extends ReadonlyArray<any>>(...elements: A) => A
Example
import { make } from "effect/Tuple"
assert.deepStrictEqual(make(1, "hello", true), [1, "hello", true])
Added in v2.0.0
getters
at
Retrieves the element at a specified index from a tuple.
Signature
export declare const at: {
<N extends number>(index: N): <A extends ReadonlyArray<unknown>>(self: A) => A[N]
<A extends ReadonlyArray<unknown>, N extends number>(self: A, index: N): A[N]
}
Example
import { Tuple } from "effect"
assert.deepStrictEqual(Tuple.at([1, "hello", true], 1), "hello")
Added in v3.4.0
getFirst
Return the first element of a tuple.
Signature
export declare const getFirst: <L, R>(self: readonly [L, R]) => L
Example
import { getFirst } from "effect/Tuple"
assert.deepStrictEqual(getFirst(["hello", 42]), "hello")
Added in v2.0.0
getSecond
Return the second element of a tuple.
Signature
export declare const getSecond: <L, R>(self: readonly [L, R]) => R
Example
import { getSecond } from "effect/Tuple"
assert.deepStrictEqual(getSecond(["hello", 42]), 42)
Added in v2.0.0
guards
isTupleOf
Determine if an Array
is a tuple with exactly N
elements, narrowing down the type to TupleOf
.
An Array
is considered to be a TupleOf
if its length is exactly N
.
Signature
export declare const isTupleOf: {
<N extends number>(n: N): <T>(self: ReadonlyArray<T>) => self is TupleOf<N, T>
<T, N extends number>(self: ReadonlyArray<T>, n: N): self is TupleOf<N, T>
}
Example
import { isTupleOf } from "effect/Tuple"
assert.deepStrictEqual(isTupleOf([1, 2, 3], 3), true)
assert.deepStrictEqual(isTupleOf([1, 2, 3], 2), false)
assert.deepStrictEqual(isTupleOf([1, 2, 3], 4), false)
const arr: number[] = [1, 2, 3]
if (isTupleOf(arr, 3)) {
console.log(arr)
// ^? [number, number, number]
}
Added in v3.3.0
isTupleOfAtLeast
Determine if an Array
is a tuple with at least N
elements, narrowing down the type to TupleOfAtLeast
.
An Array
is considered to be a TupleOfAtLeast
if its length is at least N
.
Signature
export declare const isTupleOfAtLeast: {
<N extends number>(n: N): <T>(self: ReadonlyArray<T>) => self is TupleOfAtLeast<N, T>
<T, N extends number>(self: ReadonlyArray<T>, n: N): self is TupleOfAtLeast<N, T>
}
Example
import { isTupleOfAtLeast } from "effect/Tuple"
assert.deepStrictEqual(isTupleOfAtLeast([1, 2, 3], 3), true)
assert.deepStrictEqual(isTupleOfAtLeast([1, 2, 3], 2), true)
assert.deepStrictEqual(isTupleOfAtLeast([1, 2, 3], 4), false)
const arr: number[] = [1, 2, 3, 4]
if (isTupleOfAtLeast(arr, 3)) {
console.log(arr)
// ^? [number, number, number, ...number[]]
}
Added in v3.3.0
mapping
mapBoth
Transforms both elements of a tuple using the given functions.
Signature
export declare const mapBoth: {
<L1, L2, R1, R2>(options: {
readonly onFirst: (e: L1) => L2
readonly onSecond: (a: R1) => R2
}): (self: readonly [L1, R1]) => [L2, R2]
<L1, R1, L2, R2>(
self: readonly [L1, R1],
options: { readonly onFirst: (e: L1) => L2; readonly onSecond: (a: R1) => R2 }
): [L2, R2]
}
Example
import { mapBoth } from "effect/Tuple"
assert.deepStrictEqual(mapBoth(["hello", 42], { onFirst: (s) => s.toUpperCase(), onSecond: (n) => n.toString() }), [
"HELLO",
"42"
])
Added in v2.0.0
mapFirst
Transforms the first component of a tuple using a given function.
Signature
export declare const mapFirst: {
<L1, L2>(f: (left: L1) => L2): <R>(self: readonly [L1, R]) => [L2, R]
<L1, R, L2>(self: readonly [L1, R], f: (left: L1) => L2): [L2, R]
}
Example
import { mapFirst } from "effect/Tuple"
assert.deepStrictEqual(
mapFirst(["hello", 42], (s) => s.toUpperCase()),
["HELLO", 42]
)
Added in v2.0.0
mapSecond
Transforms the second component of a tuple using a given function.
Signature
export declare const mapSecond: {
<R1, R2>(f: (right: R1) => R2): <L>(self: readonly [L, R1]) => [L, R2]
<L, R1, R2>(self: readonly [L, R1], f: (right: R1) => R2): [L, R2]
}
Example
import { mapSecond } from "effect/Tuple"
assert.deepStrictEqual(
mapSecond(["hello", 42], (n) => n.toString()),
["hello", "42"]
)
Added in v2.0.0
type lambdas
TupleTypeLambda (interface)
Signature
export interface TupleTypeLambda extends TypeLambda {
readonly type: [this["Out1"], this["Target"]]
}
Added in v2.0.0
utils
swap
Swaps the two elements of a tuple.
Signature
export declare const swap: <L, R>(self: readonly [L, R]) => [R, L]
Example
import { swap } from "effect/Tuple"
assert.deepStrictEqual(swap(["hello", 42]), [42, "hello"])
Added in v2.0.0