Tuple — typed pairs
Some values always come in pairs: a product name and its price, a city and its population, a configuration key and its current value. Tuple<A, B> is a typed alias for readonly [A, B]. Both values are always present. The library provides data-last operations for transforming either or both sides, consuming the pair into a single value, and composing these steps cleanly in a pipeline.
Creating a pair
Section titled “Creating a pair”If you already have a readonly [A, B] value — from Arr.zip, Arr.splitAt, or a native tuple
literal — it is already a valid Tuple<A, B>. You don’t need to call make to convert it.
Reading the values
Section titled “Reading the values”first and second extract a value from a pair. They are useful as the last step in a pipeline
or when you only need one side:
Transforming one side
Section titled “Transforming one side”mapFirst and mapSecond apply a function to one element and return a new pair with the other
element unchanged.
A realistic example — formatting a localised price:
Transforming both sides at once
Section titled “Transforming both sides at once”mapBoth applies two functions — one per side — in a single step:
Consuming the pair
Section titled “Consuming the pair”fold collapses both values into one by applying a binary function. It is usually the final step
in a pipeline:
Swapping the two sides
Section titled “Swapping the two sides”swap reverses the pair: [A, B] becomes [B, A]. Useful when a downstream function expects
the elements in the opposite order:
Converting to an array
Section titled “Converting to an array”toArray converts the pair to a readonly (A | B)[]. The elements stay in order:
Observing values without changing them
Section titled “Observing values without changing them”tap runs a side effect on both values and returns the pair unchanged. Use it for logging or
debugging in the middle of a pipeline:
When to use Tuple
Section titled “When to use Tuple”Use Tuple when:
- Two values always belong together and travel as a unit through a pipeline
- You want to transform one or both sides without destructuring at each step
- You are working with output from
Arr.ziporArr.splitAtand need to manipulate the pair foldprovides a clean final step to collapse the pair into a single result
Keep using native tuple destructuring when the pair is short-lived and the transformation is a
single expression — const [a, b] = pair; return f(a, b); is perfectly clear for simple cases.
Tuple earns its place in longer pipelines where the pair passes through several steps before
being consumed.