fix Construct<T>, add bimap to result

This commit is contained in:
Rowan 2025-03-31 13:37:15 -05:00
parent 34ad493fc5
commit c2b63ff779
3 changed files with 40 additions and 14 deletions

View file

@ -15,7 +15,8 @@ export function constant() { return this }
/** /**
* @template T * @template T
* @typedef {{ [Self]: (value: T) => Applicative<T> }} Applicative * @this {Construct<T>}
* @typedef {{ [Self]: <a>(value: a) => Construct<a> }} Construct
*/ */
/** /**
@ -44,10 +45,10 @@ export function extract() {
} }
/** /**
* @template T * @template T, U
* @this {Applicative<T>} * @this {Construct<T>}
* @param {T} value * @param {U} value
* @returns {Applicative<T>} * @returns {Construct<U>}
*/ */
export function of(value) { export function of(value) {
return this[Self](value) return this[Self](value)

View file

@ -1,6 +1,6 @@
import { chain, constant, Self, Value } from './common.js' import { chain, constant, Self, Value } from './common.js'
/** @import { Applicative, Morphism, Set } from './common.js' */ /** @import { Construct, Morphism, Set } from './common.js' */
/** /**
* @template T * @template T
@ -20,7 +20,7 @@ import { chain, constant, Self, Value } from './common.js'
/** /**
* @template T * @template T
* @typedef {Applicative<T> & Set<T> & SomeMethods<T>} Some * @typedef {Construct<T> & Set<T> & SomeMethods<T>} Some
* @variation 1 * @variation 1
*/ */

View file

@ -1,6 +1,6 @@
import { chain, constant, Self, Value } from './common.js' import { chain, constant, Self, Value } from './common.js'
/** @import { Morphism } from './common.js' */ /** @import { Construct, Morphism, Set } from './common.js' */
/** /**
* @template T, E * @template T, E
@ -9,24 +9,36 @@ import { chain, constant, Self, Value } from './common.js'
/** /**
* @template T * @template T
* @typedef Ok * @typedef OkMethods
* @property {typeof isOk} isOk * @property {typeof isOk} isOk
* @property {typeof isErr} isErr * @property {typeof isErr} isErr
* @property {typeof chain} chain * @property {typeof chain} chain
* @property {typeof map} map * @property {typeof map} map
* @property {typeof alt} alt * @property {typeof alt} alt
* @property {typeof fold} fold * @property {typeof fold} fold
* @property {typeof bimap} bimap
*/ */
/** /**
* @template T * @template T
* @typedef Err * @typedef {Set<T> & Construct<T> & OkMethods<T>} Ok
*/
/**
* @template T
* @typedef ErrMethods
* @property {typeof isOk} isOk * @property {typeof isOk} isOk
* @property {typeof isErr} isErr * @property {typeof isErr} isErr
* @property {typeof chain} chain * @property {typeof chain} chain
* @property {typeof map} map * @property {typeof map} map
* @property {typeof alt} alt * @property {typeof alt} alt
* @property {typeof fold} fold * @property {typeof fold} fold
* @property {typeof bimap} bimap
*/
/**
* @template T
* @typedef {Set<T> & Construct<T> & ErrMethods<T>} Err
*/ */
/** /**
@ -36,13 +48,24 @@ import { chain, constant, Self, Value } from './common.js'
* @returns {Result<U, E>} * @returns {Result<U, E>}
*/ */
function map(fn) { function map(fn) {
return this[Self](this.chain(fn)) return /** @type {Result<U, E>} */ (this[Self](this.chain(fn)))
}
/**
* @template T1, T2, E1, E2
* @this {Result<T1, E1>}
* @param {Morphism<T1, T2>} y
* @param {Morphism<E1, E2>} x
* @returns {Result<T2, E2>}
*/
function bimap(x, y) {
return map.call(this, y).map(x)
} }
/** /**
* @template T, U, E * @template T, U, E
* @this {Result<T, E>} * @this {Result<T, E>}
* @param {Morphism<T, Result<U, E>>} fn * @param {Morphism<T, U>} fn
* @param {U} acc * @param {U} acc
* @return {U} * @return {U}
*/ */
@ -92,7 +115,8 @@ export const Ok = value => Object.freeze({
chain, chain,
map, map,
alt, alt,
fold fold,
bimap
}) })
/** /**
@ -108,7 +132,8 @@ export const Err = value => Object.freeze({
alt, alt,
isOk, isOk,
isErr, isErr,
fold fold,
bimap
}) })
/** /**