stellar_prune/build/dev/javascript/gleam_stdlib/gleam/pair.mjs
2025-11-30 15:44:22 +01:00

102 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Returns the first element in a pair.
*
* ## Examples
*
* ```gleam
* first(#(1, 2))
* // -> 1
* ```
*/
export function first(pair) {
let a;
a = pair[0];
return a;
}
/**
* Returns the second element in a pair.
*
* ## Examples
*
* ```gleam
* second(#(1, 2))
* // -> 2
* ```
*/
export function second(pair) {
let a;
a = pair[1];
return a;
}
/**
* Returns a new pair with the elements swapped.
*
* ## Examples
*
* ```gleam
* swap(#(1, 2))
* // -> #(2, 1)
* ```
*/
export function swap(pair) {
let a;
let b;
a = pair[0];
b = pair[1];
return [b, a];
}
/**
* Returns a new pair with the first element having had `with` applied to
* it.
*
* ## Examples
*
* ```gleam
* #(1, 2) |> map_first(fn(n) { n * 2 })
* // -> #(2, 2)
* ```
*/
export function map_first(pair, fun) {
let a;
let b;
a = pair[0];
b = pair[1];
return [fun(a), b];
}
/**
* Returns a new pair with the second element having had `with` applied to
* it.
*
* ## Examples
*
* ```gleam
* #(1, 2) |> map_second(fn(n) { n * 2 })
* // -> #(1, 4)
* ```
*/
export function map_second(pair, fun) {
let a;
let b;
a = pair[0];
b = pair[1];
return [a, fun(b)];
}
/**
* Returns a new pair with the given elements. This can also be done using the dedicated
* syntax instead: `new(1, 2) == #(1, 2)`.
*
* ## Examples
*
* ```gleam
* new(1, 2)
* // -> #(1, 2)
* ```
*/
export function new$(first, second) {
return [first, second];
}