Skip to content

Pair

Pair: object

Defined in: Core/Pair.ts:22

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

Returns the first value from the pair.

A

B

Pair<A, B>

A

Pair.second to retrieve the second value.

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

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

Creates a Pair from two values.

A

B

A

B

Pair<A, B>

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

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

Transforms both values independently in a single step.

A

C

B

D

(first) => C

(second) => D

(pair) => Pair<C, D>

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

mapFirst: <A, C>(transform) => <B>(pair) => Pair<C, B>

Transforms the first value, leaving the second unchanged.

A

C

(first) => C

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

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

mapSecond: <B, D>(transform) => <A>(pair) => Pair<A, D>

Transforms the second value, leaving the first unchanged.

B

D

(second) => D

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

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

merge: <A, B, C>(transform) => (pair) => C

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

A

B

C

(first, second) => C

(pair) => C

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

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

Returns the second value from the pair.

A

B

Pair<A, B>

B

Pair.first to retrieve the first value.

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

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

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

A

B

Pair<A, B>

Pair<B, A>

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

tap: <A, B>(sideEffect) => (pair) => 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

(first, second) => void

(pair) => Pair<A, B>

pipe(
  Pair.make("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>(pair) => 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.make("hello", 42)); // ["hello", 42]