summaryrefslogtreecommitdiff
path: root/u/fn
diff options
context:
space:
mode:
Diffstat (limited to 'u/fn')
-rw-r--r--u/fn/callable.ts20
-rw-r--r--u/fn/either.ts100
-rw-r--r--u/fn/index.ts2
3 files changed, 0 insertions, 122 deletions
diff --git a/u/fn/callable.ts b/u/fn/callable.ts
deleted file mode 100644
index 8a61057..0000000
--- a/u/fn/callable.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-// deno-lint-ignore no-explicit-any
-export interface Callable<T = any, ArgT = any> {
- (...args: Array<ArgT>): T;
-}
-
-export interface Supplier<T> extends Callable<T, undefined> {
- (): T;
-}
-
-export interface Mapper<T, U> extends Callable<U, T> {
- (t: T): U;
-}
-
-export interface BiMapper<T, U, R> extends Callable {
- (t: T, u: U): R;
-}
-
-export interface SideEffect<T> extends Mapper<T, void> {}
-
-export interface BiSideEffect<T, U> extends BiMapper<T, U, void> {}
diff --git a/u/fn/either.ts b/u/fn/either.ts
deleted file mode 100644
index 8c47b64..0000000
--- a/u/fn/either.ts
+++ /dev/null
@@ -1,100 +0,0 @@
-import { type Mapper, type Supplier, isObject } from '@emprespresso/pengueno';
-
-type IEitherTag = 'IEither';
-const iEitherTag: IEitherTag = 'IEither';
-
-export interface _Either<LeftT, RightT, 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>>;
-}
-
-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));
- }
- 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));
- }
-}
-
-export const isEither = <E, T>(o: unknown): o is IEither<E, T> => {
- return isObject(o) && '_tag' in o && o._tag === 'IEither';
-};
diff --git a/u/fn/index.ts b/u/fn/index.ts
deleted file mode 100644
index 1ec71aa..0000000
--- a/u/fn/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from './callable.js';
-export * from './either.js';