Tuple overview
Extension to the Effect Tuple module providing safe single-element construction, element replication, and prefix prepending.
Mental model
- Tuples are fixed-length arrays whose element types are tracked individually by TypeScript.
- This module focuses on a small set of constructors and editing helpers; combine them with Effect’s
Tuplefor the rest.
Common tasks
- Construct: {@link of}, {@link replicate}
- Edit: {@link prependElement}
Quickstart
Example (Build a [1] tuple from Array.map)
import { Array, pipe } from "effect"
import * as MTuple from "@parischap/effect-lib/MTuple"
console.log(pipe([1, 2, 3], Array.map(MTuple.of))) // [[1], [2], [3]]
Table of contents
Constructors
of
Builds a single-element tuple [a].
- Prefer this over
Tuple.makeinside callbacks of variadic-aware iterators (Array.map,Array.filter, …): those iterators pass extra arguments (the index, the source) andTuple.make, being variadic, would silently include them. For example,pipe([1, 2, 3], Array.map(Tuple.make))yields[[1, 0], [2, 1], [3, 2]]rather than the probably-intended[[1], [2], [3]].
Example (Single-element tuple)
import { Array, pipe } from "effect"
import * as MTuple from "@parischap/effect-lib/MTuple"
console.log(pipe([1, 2, 3], Array.map(MTuple.of))) // [[1], [2], [3]]
Signature
export declare const of: <A>(a: A) => [A]
replicate
Builds a tuple of length n whose every element is a.
nis captured at the type level, so the result is typedTuple<A, N>.
Example (Replicate a value)
import { pipe } from "effect"
import * as MTuple from "@parischap/effect-lib/MTuple"
console.log(pipe("x", MTuple.replicate(3))) // ['x', 'x', 'x']
Signature
export declare const replicate: <N extends number>(n: N) => <A>(a: A) => MTypes.Tuple<A, N>
Utils
prependElement
Returns [that, ...self], prepending that to the head of self while preserving the tuple type.
Example (Prepend an element)
import { pipe } from "effect"
import * as MTuple from "@parischap/effect-lib/MTuple"
console.log(pipe([2, 3] as const, MTuple.prependElement(1))) // [1, 2, 3]
Signature
export declare const prependElement: <B>(that: B) => <A extends ReadonlyArray<unknown>>(self: A) => [B, ...A]