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
* @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
* @this {Applicative<T>}
* @param {T} value
* @returns {Applicative<T>}
* @template T, U
* @this {Construct<T>}
* @param {U} value
* @returns {Construct<U>}
*/
export function of(value) {
return this[Self](value)

View file

@ -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<T> & Set<T> & SomeMethods<T>} Some
* @typedef {Construct<T> & Set<T> & SomeMethods<T>} Some
* @variation 1
*/

View file

@ -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<T> & Construct<T> & OkMethods<T>} 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<T> & Construct<T> & ErrMethods<T>} Err
*/
/**
@ -36,13 +48,24 @@ import { chain, constant, Self, Value } from './common.js'
* @returns {Result<U, E>}
*/
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
* @this {Result<T, E>}
* @param {Morphism<T, Result<U, E>>} fn
* @param {Morphism<T, U>} 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
})
/**