summaryrefslogtreecommitdiff
path: root/u/fn
diff options
context:
space:
mode:
authorElizabeth Hunt <me@liz.coffee>2025-06-20 14:53:38 -0700
committerElizabeth Hunt <me@liz.coffee>2025-06-20 14:53:38 -0700
commitd4791f3d357634daf506fb8f91cc5332a794c421 (patch)
tree1bb01d2d4d8fa74d83bb6f99f2c8aa4146ca2d11 /u/fn
parentd7e8d31c94cd713a2f4cf799e20e993acc69e361 (diff)
downloadci-d4791f3d357634daf506fb8f91cc5332a794c421.tar.gz
ci-d4791f3d357634daf506fb8f91cc5332a794c421.zip
Move to nodejs
Diffstat (limited to 'u/fn')
-rw-r--r--u/fn/callable.ts8
-rw-r--r--u/fn/either.ts180
-rw-r--r--u/fn/index.ts2
-rw-r--r--u/fn/mod.ts2
4 files changed, 89 insertions, 103 deletions
diff --git a/u/fn/callable.ts b/u/fn/callable.ts
index cfb7d00..8a61057 100644
--- a/u/fn/callable.ts
+++ b/u/fn/callable.ts
@@ -1,18 +1,18 @@
// deno-lint-ignore no-explicit-any
export interface Callable<T = any, ArgT = any> {
- (...args: Array<ArgT>): T;
+ (...args: Array<ArgT>): T;
}
export interface Supplier<T> extends Callable<T, undefined> {
- (): T;
+ (): T;
}
export interface Mapper<T, U> extends Callable<U, T> {
- (t: T): U;
+ (t: T): U;
}
export interface BiMapper<T, U, R> extends Callable {
- (t: T, u: U): R;
+ (t: T, u: U): R;
}
export interface SideEffect<T> extends Mapper<T, void> {}
diff --git a/u/fn/either.ts b/u/fn/either.ts
index ffe8033..8c47b64 100644
--- a/u/fn/either.ts
+++ b/u/fn/either.ts
@@ -1,114 +1,100 @@
-import { type Mapper, type Supplier, isObject } from "@emprespresso/pengueno";
+import { type Mapper, type Supplier, isObject } from '@emprespresso/pengueno';
-type IEitherTag = "IEither";
-const iEitherTag: IEitherTag = "IEither";
+type IEitherTag = 'IEither';
+const iEitherTag: IEitherTag = 'IEither';
export interface _Either<LeftT, RightT, T> {
- readonly isLeft: LeftT;
- readonly isRight: RightT;
- readonly value: T;
+ readonly isLeft: LeftT;
+ readonly isRight: RightT;
+ readonly value: T;
}
export type Left<E> = _Either<true, false, E>;
export type Right<T> = _Either<false, true, T>;
export interface IEither<E, T> {
- readonly _tag: IEitherTag;
-
- mapBoth: <_E, _T>(
- errBranch: Mapper<E, _E>,
- okBranch: Mapper<T, _T>,
- ) => IEither<_E, _T>;
- fold: <_T>(folder: Mapper<Left<E> | Right<T>, _T>) => _T;
- moveRight: <_T>(t: _T) => IEither<E, _T>;
- mapRight: <_T>(mapper: Mapper<T, _T>) => IEither<E, _T>;
- mapLeft: <_E>(mapper: Mapper<E, _E>) => IEither<_E, T>;
- flatMap: <_T>(mapper: Mapper<T, IEither<E, _T>>) => IEither<E, _T>;
- flatMapAsync: <_T>(
- mapper: Mapper<T, Promise<IEither<E, _T>>>,
- ) => Promise<IEither<E, _T>>;
+ readonly _tag: IEitherTag;
+
+ mapBoth: <_E, _T>(errBranch: Mapper<E, _E>, okBranch: Mapper<T, _T>) => IEither<_E, _T>;
+ fold: <_T>(folder: Mapper<Left<E> | Right<T>, _T>) => _T;
+ moveRight: <_T>(t: _T) => IEither<E, _T>;
+ mapRight: <_T>(mapper: Mapper<T, _T>) => IEither<E, _T>;
+ mapLeft: <_E>(mapper: Mapper<E, _E>) => IEither<_E, T>;
+ flatMap: <_T>(mapper: Mapper<T, IEither<E, _T>>) => IEither<E, _T>;
+ flatMapAsync: <_T>(mapper: Mapper<T, Promise<IEither<E, _T>>>) => Promise<IEither<E, _T>>;
}
export class Either<E, T> implements IEither<E, T> {
- private readonly self: Left<E> | Right<T>;
-
- private constructor(
- init: { err?: E; ok?: T },
- public readonly _tag: IEitherTag = iEitherTag,
- ) {
- this.self = <Left<E> | Right<T>>{
- isLeft: "err" in init,
- isRight: "ok" in init,
- value: init.err ?? init.ok!,
- };
- }
-
- public moveRight<_T>(t: _T) {
- return this.mapRight(() => t);
- }
-
- public fold<_T>(folder: Mapper<Left<E> | Right<T>, _T>): _T {
- return folder(this.self);
- }
-
- public mapBoth<_E, _T>(
- errBranch: Mapper<E, _E>,
- okBranch: Mapper<T, _T>,
- ): IEither<_E, _T> {
- if (this.self.isLeft) return Either.left(errBranch(this.self.value));
- return Either.right(okBranch(this.self.value));
- }
-
- public flatMap<_T>(mapper: Mapper<T, IEither<E, _T>>): IEither<E, _T> {
- if (this.self.isRight) return mapper(this.self.value);
- return Either.left<E, _T>(this.self.value);
- }
-
- public mapRight<_T>(mapper: Mapper<T, _T>): IEither<E, _T> {
- if (this.self.isRight) return Either.right<E, _T>(mapper(this.self.value));
- return Either.left<E, _T>(this.self.value);
- }
-
- public mapLeft<_E>(mapper: Mapper<E, _E>): IEither<_E, T> {
- if (this.self.isLeft) return Either.left<_E, T>(mapper(this.self.value));
- return Either.right<_E, T>(this.self.value);
- }
-
- public async flatMapAsync<_T>(
- mapper: Mapper<T, Promise<IEither<E, _T>>>,
- ): Promise<IEither<E, _T>> {
- if (this.self.isLeft) {
- return Promise.resolve(Either.left<E, _T>(this.self.value));
+ private readonly self: Left<E> | Right<T>;
+
+ private constructor(
+ init: { err?: E; ok?: T },
+ public readonly _tag: IEitherTag = iEitherTag,
+ ) {
+ this.self = <Left<E> | Right<T>>{
+ isLeft: 'err' in init,
+ isRight: 'ok' in init,
+ value: init.err ?? init.ok!,
+ };
}
- return await mapper(this.self.value).catch((err) =>
- Either.left<E, _T>(err),
- );
- }
-
- static left<E, T>(e: E): IEither<E, T> {
- return new Either<E, T>({ err: e });
- }
-
- static right<E, T>(t: T): IEither<E, T> {
- return new Either<E, T>({ ok: t });
- }
-
- static fromFailable<E, T>(s: Supplier<T>): IEither<E, T> {
- try {
- return Either.right<E, T>(s());
- } catch (e) {
- return Either.left<E, T>(e as E);
+
+ public moveRight<_T>(t: _T) {
+ return this.mapRight(() => t);
+ }
+
+ public fold<_T>(folder: Mapper<Left<E> | Right<T>, _T>): _T {
+ return folder(this.self);
+ }
+
+ public mapBoth<_E, _T>(errBranch: Mapper<E, _E>, okBranch: Mapper<T, _T>): IEither<_E, _T> {
+ if (this.self.isLeft) return Either.left(errBranch(this.self.value));
+ return Either.right(okBranch(this.self.value));
+ }
+
+ public flatMap<_T>(mapper: Mapper<T, IEither<E, _T>>): IEither<E, _T> {
+ if (this.self.isRight) return mapper(this.self.value);
+ return Either.left<E, _T>(this.self.value);
+ }
+
+ public mapRight<_T>(mapper: Mapper<T, _T>): IEither<E, _T> {
+ if (this.self.isRight) return Either.right<E, _T>(mapper(this.self.value));
+ return Either.left<E, _T>(this.self.value);
+ }
+
+ public mapLeft<_E>(mapper: Mapper<E, _E>): IEither<_E, T> {
+ if (this.self.isLeft) return Either.left<_E, T>(mapper(this.self.value));
+ return Either.right<_E, T>(this.self.value);
+ }
+
+ public async flatMapAsync<_T>(mapper: Mapper<T, Promise<IEither<E, _T>>>): Promise<IEither<E, _T>> {
+ if (this.self.isLeft) {
+ return Promise.resolve(Either.left<E, _T>(this.self.value));
+ }
+ return await mapper(this.self.value).catch((err) => Either.left<E, _T>(err));
+ }
+
+ static left<E, T>(e: E): IEither<E, T> {
+ return new Either<E, T>({ err: e });
+ }
+
+ static right<E, T>(t: T): IEither<E, T> {
+ return new Either<E, T>({ ok: t });
+ }
+
+ static fromFailable<E, T>(s: Supplier<T>): IEither<E, T> {
+ try {
+ return Either.right<E, T>(s());
+ } catch (e) {
+ return Either.left<E, T>(e as E);
+ }
+ }
+
+ static async fromFailableAsync<E, T>(s: Supplier<Promise<T>> | Promise<T>): Promise<IEither<E, T>> {
+ return await (typeof s === 'function' ? s() : s)
+ .then((t: T) => Either.right<E, T>(t))
+ .catch((e: E) => Either.left<E, T>(e));
}
- }
-
- static async fromFailableAsync<E, T>(
- s: Supplier<Promise<T>>,
- ): Promise<IEither<E, T>> {
- return await s()
- .then((t: T) => Either.right<E, T>(t))
- .catch((e: E) => Either.left<E, T>(e));
- }
}
export const isEither = <E, T>(o: unknown): o is IEither<E, T> => {
- return isObject(o) && "_tag" in o && o._tag === "IEither";
+ return isObject(o) && '_tag' in o && o._tag === 'IEither';
};
diff --git a/u/fn/index.ts b/u/fn/index.ts
new file mode 100644
index 0000000..1ec71aa
--- /dev/null
+++ b/u/fn/index.ts
@@ -0,0 +1,2 @@
+export * from './callable.js';
+export * from './either.js';
diff --git a/u/fn/mod.ts b/u/fn/mod.ts
deleted file mode 100644
index f0fbe88..0000000
--- a/u/fn/mod.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from "./callable.ts";
-export * from "./either.ts";