From dc4ac7742690f8f2bd759d57108ac4455e717fe9 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 20 Jul 2025 13:03:39 -0700 Subject: Mount src directory from path on host running worker container --- u/types/fn/either.ts | 56 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 19 deletions(-) (limited to 'u/types/fn/either.ts') diff --git a/u/types/fn/either.ts b/u/types/fn/either.ts index aa67d41..80f32b4 100644 --- a/u/types/fn/either.ts +++ b/u/types/fn/either.ts @@ -1,18 +1,36 @@ -import { BiMapper, IOptional, type Mapper, Optional, type Supplier, Tagged, isTagged } from '@emprespresso/pengueno'; +import { + BiMapper, + IOptional, + type Mapper, + Optional, + Predicate, + type Supplier, + Tagged, + isTagged, +} from '@emprespresso/pengueno'; export const IEitherTag = 'IEither' as const; export type IEitherTag = typeof IEitherTag; export const isEither = (o: unknown): o is IEither => isTagged(o, IEitherTag); export interface IEither extends Tagged { - readonly mapBoth: <_E, _T>(errBranch: Mapper, okBranch: Mapper) => IEither<_E, _T>; - readonly fold: <_T>(leftFolder: Mapper, rightFolder: Mapper) => _T; readonly left: Supplier>; readonly right: Supplier>; - readonly moveRight: <_T>(t: _T) => IEither; + readonly mapRight: <_T>(mapper: Mapper) => IEither; + readonly filter: (mapper: Predicate) => IEither; readonly mapLeft: <_E>(mapper: Mapper) => IEither<_E, T>; + readonly mapBoth: <_E, _T>(errBranch: Mapper, okBranch: Mapper) => IEither<_E, _T>; + readonly flatMap: <_T>(mapper: Mapper>) => IEither; readonly flatMapAsync: <_T>(mapper: Mapper>>) => Promise>; + + readonly moveRight: <_T>(t: _T) => IEither; + readonly fold: <_T>(leftFolder: Mapper, rightFolder: Mapper) => _T; + readonly joinRight: (other: IEither, mapper: BiMapper) => IEither; + readonly joinRightAsync: ( + other: Supplier>> | Promise>, + mapper: BiMapper, + ) => Promise>; } const ELeftTag = 'E.Left' as const; @@ -62,6 +80,11 @@ export class Either extends _Tagged implements IEither { return Either.left(this.self.err); } + public filter(mapper: Predicate): IEither { + if (isLeft(this.self)) return Either.left(this.self.err); + return Either.fromFailable(() => this.right().filter(mapper).get()); + } + public async flatMapAsync<_T>(mapper: Mapper>>): Promise> { if (isLeft(this.self)) return Promise.resolve(Either.left(this.self.err)); return await mapper(this.self.ok).catch((err) => Either.left(err)); @@ -82,23 +105,18 @@ export class Either extends _Tagged implements IEither { return Optional.none(); } - static joinRight( - arr: Array, - mapper: BiMapper>, - init: IEither, - ): IEither { - return arr.reduce((acc: IEither, x: K) => acc.flatMap((t) => mapper(x, t)), init); + public joinRight(other: IEither, mapper: BiMapper) { + return this.flatMap((t) => other.mapRight((o) => mapper(o, t))); } - static joinRightAsync( - arr: Array, - mapper: BiMapper>>, - init: IEither, - ): Promise> { - return arr.reduce( - (acc: Promise>, x: K) => acc.then((res) => res.flatMapAsync((t) => mapper(x, t))), - Promise.resolve(init), - ); + public joinRightAsync( + other: Supplier>> | Promise>, + mapper: BiMapper, + ) { + return this.flatMapAsync(async (t) => { + const o = typeof other === 'function' ? other() : other; + return o.then((other) => other.mapRight((o) => mapper(o, t))); + }); } static left(e: E): IEither { -- cgit v1.2.3-70-g09d2