Skip to content

Pair

Pair: object

Defined in: Core/Pair.ts:23

first: <A, B>(p) => A

Returns the first value from the pair.

A

B

Pair<A, B>

A

Pair.first(Pair.from.pair("Paris", 2_161_000)); // "Paris"

fold: <A, B, C>(f) => (p) => C

Applies a binary function to both values, collapsing the pair into a single value. Useful as the final step when consuming a pair in a pipeline.

A

B

C

(a, b) => C

(p) => C

pipe(Pair.from.pair("Alice", 100), Pair.fold((name, score) => `${name}: ${score}`));
// "Alice: 100"

from: object

array: <A, B>(arr) => Pair<A, B> = makeArray

Creates a Pair from a two-element array.

A

B

readonly [A, B]

Pair<A, B>

Pair.from.array(["Paris", 2_161_000] as const); // ["Paris", 2161000]

pair: <A, B>(first, second) => Pair<A, B> = makePair

Creates a Pair from two values.

A

B

A

B

Pair<A, B>

Pair.from.pair("Paris", 2_161_000); // ["Paris", 2161000]

mapBoth: <A, C, B, D>(onFirst, onSecond) => (p) => Pair<C, D>

Transforms both values independently in a single step.

A

C

B

D

(a) => C

(b) => D

(p) => Pair<C, D>

pipe(
  Pair.from.pair("alice", 42),
  Pair.mapBoth(
    (name) => name.toUpperCase(),
    (score) => score * 2,
  ),
); // ["ALICE", 84]

mapFirst: <A, C>(f) => <B>(p) => Pair<C, B>

Transforms the first value, leaving the second unchanged.

A

C

(a) => C

<B>(p) => Pair<C, B>

pipe(Pair.from.pair("alice", 42), Pair.mapFirst((s) => s.toUpperCase())); // ["ALICE", 42]

mapSecond: <B, D>(f) => <A>(p) => Pair<A, D>

Transforms the second value, leaving the first unchanged.

B

D

(b) => D

<A>(p) => Pair<A, D>

pipe(Pair.from.pair("alice", 42), Pair.mapSecond((n) => n * 2)); // ["alice", 84]

second: <A, B>(p) => B

Returns the second value from the pair.

A

B

Pair<A, B>

B

Pair.second(Pair.from.pair("Paris", 2_161_000)); // 2161000

swap: <A, B>(p) => Pair<B, A>

Swaps the two values: [A, B] becomes [B, A].

A

B

Pair<A, B>

Pair<B, A>

Pair.swap(Pair.from.pair("key", 1)); // [1, "key"]

tap: <A, B>(f) => (p) => Pair<A, B>

Runs a side effect with both values without changing the pair. Useful for logging or debugging in the middle of a pipeline.

A

B

(a, b) => void

(p) => Pair<A, B>

pipe(
  Pair.from.pair("Paris", 2_161_000),
  Pair.tap((city, pop) => console.log(`${city}: ${pop}`)),
  Pair.mapSecond((n) => n / 1_000_000),
); // logs "Paris: 2161000", returns ["Paris", 2.161]

to: object

Array: <A, B>(p) => readonly (A | B)[]

Converts the pair to a heterogeneous readonly array readonly (A | B)[].

A

B

Pair<A, B>

readonly (A | B)[]

Pair.to.Array(Pair.from.pair("hello", 42)); // ["hello", 42]