/** * Pair represents a pair of values. It can be thought of as a tuple * of two, or first and second, or separated values. * * @module Pair * @since 2.0.0 */ import "./_dnt.polyfills.js"; import type { $, Kind, Out } from "./kind.js"; import type { Applicable } from "./applicable.js"; import type { Bimappable } from "./bimappable.js"; import type { Combinable } from "./combinable.js"; import type { Comparable } from "./comparable.js"; import type { Flatmappable } from "./flatmappable.js"; import type { Foldable } from "./foldable.js"; import type { Initializable } from "./initializable.js"; import type { Mappable } from "./mappable.js"; import type { Showable } from "./showable.js"; import type { Sortable } from "./sortable.js"; import type { Traversable } from "./traversable.js"; /** * Pair represents a pair of values. This is * equivalent to a Tuple of length two, the * Separated type in fp-ts, and any other type * that contains exactly two covariant other * types. * * The primary use fo Pair in this library * is the target of a partition, where some * type A is partitioned, either into * [A, A], or [A, B] where B extends A. * * Other uses will likely come when Arrows * are implemented in fun. * * @since 2.0.0 */ export type Pair = readonly [A, B]; /** * Specifies Pair as a Higher Kinded Type, with covariant * parameters A and B corresponding to the 0th and 1st * index of any Substitutions. * * @since 2.0.0 */ export interface KindPair extends Kind { readonly kind: Pair, Out>; } /** * Creates a Pair from two values first and second with types * A and B respectively. Used to quickly construct a Pair. * * @example * ```ts * import * as P from "./pair.ts"; * * const nameAndAge = P.pair("Brandon", 37); * * const name = P.getFirst(nameAndAge); // "Brandon" * const age = P.getSecond(nameAndAge); // 37 * ``` * * @since 2.0.0 */ export declare function pair(first: A, second: B): Pair; /** * Creates a pair from a single type * * @example * ```ts * import { dup } from "./pair.ts"; * * const result = dup(1); // [1, 1] * ``` * * @since 2.0.0 */ export declare function dup(a: A): Pair; /** * Apply a function in the first position of a pair to a value * in the second position of a pair. * * @example * ```ts * import * as P from "./pair.ts"; * import { flow } from "./fn.ts"; * * const double = flow( * P.dup, * P.map(n => (m: number) => n + m), * P.merge, * ); * * const result1 = double(1); // 2 * const result2 = double(2); // 4 * ``` * * @since 2.0.0 */ export declare function merge(ua: Pair<(a: A) => I, A>): I; /** * Apply a function in the first position of a pair to a value * in the second position of a pair. * * @example * ```ts * import * as P from "./pair.ts"; * import { flow } from "./fn.ts"; * * const double = flow( * P.dup, * P.mapSecond(n => (m: number) => n + m), * P.mergeSecond, * ); * * const result1 = double(1); // 2 * const result2 = double(2); // 4 * ``` * * @since 2.0.0 */ export declare function mergeSecond(ua: Pair I>): I; /** * Extracts the first value from a Pair. * * @example * ```ts * import * as P from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const shouldBe1 = pipe( * P.pair(1, 2), * P.getFirst * ); // 1 * ``` * * @since 2.0.0 */ export declare function getFirst([first]: Pair): A; /** * Extracts the second value from a Pair. * * @example * ```ts * import * as P from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const shouldBe2 = pipe( * P.pair(1, 2), * P.getSecond * ); // 2 * ``` * * @since 2.0.0 */ export declare function getSecond([_, second]: Pair): B; /** * A curried form of the pair constructor, starting with the first * value of a pair. * * @example * ```ts * import * as P from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const result = pipe( * 37, * P.first("Brandon"), * P.mapSecond(n => n + 1), * ); // ["Brandon", 38] * ``` * * @since 2.0.0 */ export declare function first(first: A): (second: B) => Pair; /** * A curried form of the pair constructor, starting with the second * value of a pair. * * @example * ```ts * import * as P from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const result = pipe( * 37, * P.second("Brandon"), * P.map(n => n + 1), * ); // [38, "Brandon"] * ``` * * @since 2.0.0 */ export declare function second(second: B): (first: A) => Pair; /** * Creates a new Pair with the first and second values swapped. * * @example * ```ts * import * as P from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const shouldBe2 = pipe( * P.pair(1, 2), * P.swap, * P.first * ); // 2 * ``` * * @since 2.0.0 */ export declare function swap([first, second]: Pair): Pair; /** * Creates a new Pair with the same second value and a new first * value determined by the output of the fai function. * * @example * ```ts * import * as P from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const result = pipe( * P.pair(1, 2), * P.map(String), * ); // ['1', 2] * ``` * * @since 2.0.0 */ export declare function map(fai: (a: A) => I): (ta: Pair) => Pair; /** * Creates a new Pair with the same first value and a new second * value determined by the output of the fbj function. * * @example * ```ts * import * as P from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const result = pipe( * P.pair(1, 2), * P.mapSecond(String), * ); // [1, '2'] * ``` * * @since 2.0.0 */ export declare function mapSecond(fbj: (a: B) => J): (ta: Pair) => Pair; /** * Creates a new pair by mapping first through the fai * function and second through the fbj function. * * @example * ```ts * import * as P from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const result = pipe( * P.pair(1, 2), * P.bimap(String, n => n + 1), * ); // ['1', 3] * ``` * * @since 2.0.0 */ export declare function bimap(fbj: (b: B) => J, fai: (a: A) => I): (ta: Pair) => Pair; /** * Just like the first function, unwrap returns the first * value in a pair. * * @example * ```ts * import { pair, unwrap } from "./pair.ts"; * * const result = unwrap(pair(1, 2)); // 1 * ``` * * @since 2.0.0 */ export declare function unwrap([first]: Pair): A; /** * Reduces a pair with an initial value, also passing * the second value into the foldr as well. * * @example * ```ts * import { pair, fold } from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const result = pipe( * pair(10, 20), * fold(Math.max, Number.NEGATIVE_INFINITY), * ); // 20 * ``` * * @since 2.0.0 */ export declare function fold(foao: (acc: O, first: A, second: B) => O, initial: O): (ua: Pair) => O; /** * Traverse a pair using another algebraic structure's Applicable. * * @example * ```ts * import { traverse, pair } from "./pair.ts"; * import { some, ApplicableOption, fromPredicate } from "./option.ts"; * import { pipe } from "./fn.ts"; * * const traverseOption = traverse(ApplicableOption); * const startsWithB = fromPredicate( * (name: string) => name.startsWith("B") * ); * * const result1 = pipe( * pair("Brandon", 37), * traverseOption(startsWithB), * ); // { tag: "Some", value: ["Brandon", 37] } * * const result2 = pipe( * pair("Alice", 37), * traverseOption(startsWithB), * ); // { tag: "None" } * ``` * * @since 2.0.0 */ export declare function traverse(A: Applicable): (favi: (a: A) => $) => (ua: Pair) => $, J, K], [L], [M]>; /** * Creates a Showable instance for a pair, wrapping the Showable instances provided * for the first and second values. * * @since 2.0.0 */ export declare function getShowablePair(SA: Showable, SB: Showable): Showable>; /** * @since 2.0.0 */ export declare function getCombinablePair(CA: Combinable, CB: Combinable): Combinable>; /** * @since 2.0.0 */ export declare function getInitializablePair(IA: Initializable, IB: Initializable): Initializable>; /** * @since 2.0.0 */ export declare function getComparablePair(CA: Comparable, CB: Comparable): Comparable>; /** * @since 2.0.0 */ export declare function getSortablePair(SA: Sortable, SB: Sortable): Sortable>; /** * A Kind implementation used to fix the second parameter in a Pair. * Otherwise it operates the same as Pair does. * * @since 2.0.0 */ export interface KindRightPair extends Kind { readonly kind: Pair, B>; } /** * Creates a Flatmappable instance for Pair where the second parameter is * concatenated according to the Monoid instance passed in. * * @example * ```ts * import { InitializableNumberSum } from "./number.ts"; * import { getRightFlatmappable, pair } from "./pair.ts"; * import { pipe } from "./fn.ts"; * * const Flatmappable = getRightFlatmappable(InitializableNumberSum); * * const ageOneYear = (name: string) => pair(name, 1); * * const result = pipe( * pair("Brandon", 36), // Pair(Name, Age) * Flatmappable.flatmap(ageOneYear), * Flatmappable.flatmap(ageOneYear) * ); // ["Brandon", 38] * ``` * * @since 2.0.0 */ export declare function getRightFlatmappable(I: Initializable): Flatmappable>; /** * The canonical Mappable instance for Pair. Contains the * map method. * * @since 2.0.0 */ export declare const MappablePair: Mappable; /** * The canonical Bimappable instance for Pair. Contains the * bimap and mapSecond methods. * * @since 2.0.0 */ export declare const BimappablePair: Bimappable; /** * The canonical Foldable instance for Pair. Contains the * fold method. * * @since 2.0.0 */ export declare const FoldablePair: Foldable; /** * @since 2.0.0 */ export declare const TraversablePair: Traversable;