Skip to main content
Module

x/functional/library/Pair.js

Common Functional Programming Algebraic data types for JavaScript that is compatible with most modern browsers and Deno.
Go to Latest
File
import { factorizeType } from "./factories.js";
/** * The `Pair` type represents two values. * * The `Pair` type implements the following algebras: * - [x] Bifunctor * - [x] Functor * * ### Example * * ```js * const pair = Pair(42, 42) * .bimap( * x => x * 2, * x => x + 2 * ); * * assert(Pair.is(pair)); * assert(pair.first === 84); * assert(pair.first === 44); * ``` */
export const Pair = factorizeType("Pair", [ "first", "second" ]);
Pair.prototype.bimap = Pair.prototype["fantasy-land/bimap"] = function (unaryFunctionA, unaryFunctionB) {
return Pair(unaryFunctionA(this.first), unaryFunctionB(this.second));};
Pair.prototype.map = Pair.prototype["fantasy-land/map"] = function (unaryFunction) {
return Pair(unaryFunction(this.first), this.second);};
export default Pair;