From f008c002915ee19b5bf93b6cae648f10ca861a22 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Thu, 3 Jul 2025 13:14:29 -0700 Subject: Adds join functions to Eithers --- u/types/fn/either.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'u/types') diff --git a/u/types/fn/either.ts b/u/types/fn/either.ts index 6140ada..aa67d41 100644 --- a/u/types/fn/either.ts +++ b/u/types/fn/either.ts @@ -1,4 +1,4 @@ -import { IOptional, type Mapper, Optional, type Supplier, Tagged, isTagged } from '@emprespresso/pengueno'; +import { BiMapper, IOptional, type Mapper, Optional, type Supplier, Tagged, isTagged } from '@emprespresso/pengueno'; export const IEitherTag = 'IEither' as const; export type IEitherTag = typeof IEitherTag; @@ -82,6 +82,25 @@ 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); + } + + 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), + ); + } + static left(e: E): IEither { return new Either({ err: e, _tag: ELeftTag }); } -- cgit v1.2.3-70-g09d2