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/callable.ts | 2 ++ u/types/fn/either.ts | 56 +++++++++++++++++++++++++++++++++----------------- u/types/fn/optional.ts | 8 ++++---- 3 files changed, 43 insertions(+), 23 deletions(-) (limited to 'u/types/fn') diff --git a/u/types/fn/callable.ts b/u/types/fn/callable.ts index 51756d7..60d747b 100644 --- a/u/types/fn/callable.ts +++ b/u/types/fn/callable.ts @@ -10,6 +10,8 @@ export interface Mapper extends Callable { (t: T): U; } +export interface Predicate extends Mapper {} + export interface BiMapper extends Callable { (t: T, u: U): R; } 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 { diff --git a/u/types/fn/optional.ts b/u/types/fn/optional.ts index 3396a45..504e496 100644 --- a/u/types/fn/optional.ts +++ b/u/types/fn/optional.ts @@ -1,4 +1,4 @@ -import { type Mapper, type Supplier, Tagged, isTagged } from '@emprespresso/pengueno'; +import { type Mapper, Predicate, type Supplier, Tagged, isTagged } from '@emprespresso/pengueno'; export type MaybeGiven = T | undefined | null; @@ -9,7 +9,7 @@ export class IOptionalEmptyError extends Error {} export interface IOptional = NonNullable> extends Tagged, Iterable { readonly move: <_T>(t: MaybeGiven<_T>) => IOptional<_T>; readonly map: <_T>(mapper: Mapper>) => IOptional<_T>; - readonly filter: (mapper: Mapper) => IOptional; + readonly filter: (mapper: Predicate) => IOptional; readonly flatMap: <_T>(mapper: Mapper>>) => IOptional<_T>; readonly orSome: (supplier: Supplier>) => IOptional; readonly get: Supplier; @@ -48,11 +48,11 @@ export class Optional = NonNullable> extends _Tag } public get(): T { - if (isNone(this.self)) throw new IOptionalEmptyError('empty value'); + if (isNone(this.self)) throw new IOptionalEmptyError('called get() on None optional'); return this.self.value; } - public filter(mapper: Mapper): IOptional { + public filter(mapper: Predicate): IOptional { if (isNone(this.self) || !mapper(this.self.value)) return Optional.none(); return Optional.some(this.self.value); } -- cgit v1.2.3-70-g09d2