Skip to content

aivi.pair ​

Utilities for working with two-element tuples. Pairs are written (A, B) and are the primary way to group two values of potentially different types.

aivi
use aivi.pair (
    first
    second
    mapFirst
    mapSecond
    swap
    mapBoth
    toPair
    duplicate
)

At a glance ​

Canonical pair helpers ​

FunctionTypeUse it for
first(A, B) -> AExtract the left item
second(A, B) -> BExtract the right item
swap(A, B) -> (B, A)Reverse the pair order
mapFirst(A -> C) -> (A, B) -> (C, B)Transform only the left item
mapSecond(B -> C) -> (A, B) -> (A, C)Transform only the right item
mapBoth(A -> C) -> (B -> D) -> (A, B) -> (C, D)Transform both sides with separate functions
toPairA -> B -> (A, B)Named pairing combinator
duplicateA -> (A, A)Put the same value on both sides

first ​

Extracts the first element of a pair.

aivi
value key : Text = first ("Ada", 42)
// "Ada"

second ​

Extracts the second element of a pair.

aivi
type (Text, Int) -> Int
func getValue =
  |> second

swap ​

Swaps the two elements of a pair, returning (B, A) from (A, B).

aivi
use aivi.pair (swap)

type (Text, Int) -> (Int, Text)
func flipEntry =
  |> swap

mapFirst ​

Applies a function to the first element, leaving the second unchanged.

aivi
use aivi.math (square)

type (Int, Text) -> (Int, Text)
func squareFst =
  |> mapFirst square

mapSecond ​

Applies a function to the second element, leaving the first unchanged.

aivi
use aivi.math (abs)

type (Text, Int) -> (Text, Int)
func absValue =
  |> mapSecond abs

mapBoth ​

Applies one function to the first element and another to the second.

aivi
use aivi.pair (mapBoth)

use aivi.math (
    abs
    negate
)

type (Int, Int) -> (Int, Int)
func normalizePair =
  |> mapBoth abs negate

toPair ​

Constructs a pair from two separate values. Useful as a named combinator when pairing results in a pipeline.

aivi
use aivi.pair (toPair)

type Text -> Int -> (Text, Int)
func labelScore = label score =>
    toPair label score

duplicate ​

Creates a pair where both elements are the same value.

aivi
use aivi.pair (duplicate)

type Int -> (Int, Int)
func mirror =
  |> duplicate

(c) 2026 by Andreas Herd