From c2b63ff77961065191e6e31f98abf540dda2e845 Mon Sep 17 00:00:00 2001 From: rowan Date: Mon, 31 Mar 2025 13:37:15 -0500 Subject: [PATCH] fix Construct, add bimap to result --- src/algebra/common.js | 11 ++++++----- src/algebra/option.js | 4 ++-- src/algebra/result.js | 39 ++++++++++++++++++++++++++++++++------- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/algebra/common.js b/src/algebra/common.js index 2a40692..890458d 100644 --- a/src/algebra/common.js +++ b/src/algebra/common.js @@ -15,7 +15,8 @@ export function constant() { return this } /** * @template T - * @typedef {{ [Self]: (value: T) => Applicative }} Applicative + * @this {Construct} + * @typedef {{ [Self]: (value: a) => Construct }} Construct */ /** @@ -44,10 +45,10 @@ export function extract() { } /** - * @template T - * @this {Applicative} - * @param {T} value - * @returns {Applicative} + * @template T, U + * @this {Construct} + * @param {U} value + * @returns {Construct} */ export function of(value) { return this[Self](value) diff --git a/src/algebra/option.js b/src/algebra/option.js index 1187cf5..1d32cf7 100644 --- a/src/algebra/option.js +++ b/src/algebra/option.js @@ -1,6 +1,6 @@ import { chain, constant, Self, Value } from './common.js' -/** @import { Applicative, Morphism, Set } from './common.js' */ +/** @import { Construct, Morphism, Set } from './common.js' */ /** * @template T @@ -20,7 +20,7 @@ import { chain, constant, Self, Value } from './common.js' /** * @template T - * @typedef {Applicative & Set & SomeMethods} Some + * @typedef {Construct & Set & SomeMethods} Some * @variation 1 */ diff --git a/src/algebra/result.js b/src/algebra/result.js index f387be2..64f1448 100644 --- a/src/algebra/result.js +++ b/src/algebra/result.js @@ -1,6 +1,6 @@ import { chain, constant, Self, Value } from './common.js' -/** @import { Morphism } from './common.js' */ +/** @import { Construct, Morphism, Set } from './common.js' */ /** * @template T, E @@ -9,24 +9,36 @@ import { chain, constant, Self, Value } from './common.js' /** * @template T - * @typedef Ok + * @typedef OkMethods * @property {typeof isOk} isOk * @property {typeof isErr} isErr * @property {typeof chain} chain * @property {typeof map} map * @property {typeof alt} alt * @property {typeof fold} fold + * @property {typeof bimap} bimap */ /** * @template T - * @typedef Err + * @typedef {Set & Construct & OkMethods} Ok + */ + +/** + * @template T + * @typedef ErrMethods * @property {typeof isOk} isOk * @property {typeof isErr} isErr * @property {typeof chain} chain * @property {typeof map} map * @property {typeof alt} alt * @property {typeof fold} fold + * @property {typeof bimap} bimap + */ + +/** + * @template T + * @typedef {Set & Construct & ErrMethods} Err */ /** @@ -36,13 +48,24 @@ import { chain, constant, Self, Value } from './common.js' * @returns {Result} */ function map(fn) { - return this[Self](this.chain(fn)) + return /** @type {Result} */ (this[Self](this.chain(fn))) +} + +/** + * @template T1, T2, E1, E2 + * @this {Result} + * @param {Morphism} y + * @param {Morphism} x + * @returns {Result} + */ +function bimap(x, y) { + return map.call(this, y).map(x) } /** * @template T, U, E * @this {Result} - * @param {Morphism>} fn + * @param {Morphism} fn * @param {U} acc * @return {U} */ @@ -92,7 +115,8 @@ export const Ok = value => Object.freeze({ chain, map, alt, - fold + fold, + bimap }) /** @@ -108,7 +132,8 @@ export const Err = value => Object.freeze({ alt, isOk, isErr, - fold + fold, + bimap }) /**