summaryrefslogtreecommitdiff
path: root/u/types
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2025-07-03 13:14:29 -0700
committerElizabeth Hunt <me@liz.coffee>2025-07-03 13:14:29 -0700
commitf008c002915ee19b5bf93b6cae648f10ca861a22 (patch)
tree6eb9708884ad351c6ae2f38bf5a3035df1f0d54d /u/types
parentb2327674c851c9942e8eb2f94b1b5f1524521a19 (diff)
downloadci-f008c002915ee19b5bf93b6cae648f10ca861a22.tar.gz
ci-f008c002915ee19b5bf93b6cae648f10ca861a22.zip
Adds join functions to Eithers
Diffstat (limited to 'u/types')
-rw-r--r--u/types/fn/either.ts21
1 files changed, 20 insertions, 1 deletions
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<E, T> extends _Tagged implements IEither<E, T> {
return Optional.none();
}
+ static joinRight<K, E, T>(
+ arr: Array<K>,
+ mapper: BiMapper<K, T, IEither<E, T>>,
+ init: IEither<E, T>,
+ ): IEither<E, T> {
+ return arr.reduce((acc: IEither<E, T>, x: K) => acc.flatMap((t) => mapper(x, t)), init);
+ }
+
+ static joinRightAsync<K, E, T>(
+ arr: Array<K>,
+ mapper: BiMapper<K, T, Promise<IEither<E, T>>>,
+ init: IEither<E, T>,
+ ): Promise<IEither<E, T>> {
+ return arr.reduce(
+ (acc: Promise<IEither<E, T>>, x: K) => acc.then((res) => res.flatMapAsync((t) => mapper(x, t))),
+ Promise.resolve(init),
+ );
+ }
+
static left<E, T>(e: E): IEither<E, T> {
return new Either({ err: e, _tag: ELeftTag });
}