From ef51b25e4388cbdf3a27e23d9f1fa381ae20a5ad Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Fri, 16 May 2025 16:17:13 -0700 Subject: snapshot --- u/fn/callable.ts | 4 ++++ u/fn/either.ts | 28 ++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) (limited to 'u/fn') diff --git a/u/fn/callable.ts b/u/fn/callable.ts index 2749947..a087928 100644 --- a/u/fn/callable.ts +++ b/u/fn/callable.ts @@ -13,3 +13,7 @@ export interface SideEffect extends Callable { export interface Mapper extends Callable { (t: T): U; } + +export interface BiMapper extends Callable { + (t: T, u: U): R; +} diff --git a/u/fn/either.ts b/u/fn/either.ts index 916bb71..9dc1027 100644 --- a/u/fn/either.ts +++ b/u/fn/either.ts @@ -1,10 +1,16 @@ -import type { Mapper, Supplier } from "@emprespresso/pengueno"; +import type { BiMapper, Mapper, Supplier } from "@emprespresso/pengueno"; +import { isObject } from "../leftpadesque/mod.ts"; + +type IEitherTag = "IEither"; +const iEitherTag: IEitherTag = "IEither"; export interface IEither { + readonly _tag: IEitherTag; mapBoth: ( errBranch: Mapper, okBranch: Mapper, ) => IEither; + fold: (folder: BiMapper) => Tt; moveRight: (t: Tt) => IEither; mapRight: (mapper: Mapper) => IEither; mapLeft: (mapper: Mapper) => IEither; @@ -15,14 +21,20 @@ export interface IEither { } export class Either implements IEither { - private constructor(private readonly err?: E, private readonly ok?: T) {} + private constructor( + private readonly err?: E, + private readonly ok?: T, + public readonly _tag: IEitherTag = iEitherTag, + ) {} - public moveRight( - t: Tt, - ) { + public moveRight(t: Tt) { return this.mapRight(() => t); } + public fold(folder: BiMapper): R { + return folder(this.err ?? null, this.ok ?? null); + } + public mapBoth( errBranch: Mapper, okBranch: Mapper, @@ -37,7 +49,7 @@ export class Either implements IEither { } public mapRight(mapper: Mapper): IEither { - if (this.ok !== undefined) return Either.right(mapper(this.ok)); + if (this.ok !== undefined) return Either.right(mapper(this.ok)); return Either.left(this.err!); } @@ -79,3 +91,7 @@ export class Either implements IEither { } } } + +export const isEither = (o: unknown): o is IEither => { + return isObject(o) && "_tag" in o && o._tag === "IEither"; +}; -- cgit v1.2.3-70-g09d2