From 7aa11b7a8abacf81dec20fff21216df35d333756 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 27 Jul 2025 18:50:33 -0700 Subject: Pulls in pengueno from npm --- u/types/collections/cons.ts | 40 ----------- u/types/collections/index.ts | 2 - u/types/collections/list_zipper.ts | 70 ------------------ u/types/fn/callable.ts | 21 ------ u/types/fn/either.ts | 143 ------------------------------------- u/types/fn/index.ts | 3 - u/types/fn/optional.ts | 93 ------------------------ u/types/index.ts | 7 -- u/types/misc.ts | 3 - u/types/object.ts | 1 - u/types/tagged.ts | 8 --- 11 files changed, 391 deletions(-) delete mode 100644 u/types/collections/cons.ts delete mode 100644 u/types/collections/index.ts delete mode 100644 u/types/collections/list_zipper.ts delete mode 100644 u/types/fn/callable.ts delete mode 100644 u/types/fn/either.ts delete mode 100644 u/types/fn/index.ts delete mode 100644 u/types/fn/optional.ts delete mode 100644 u/types/index.ts delete mode 100644 u/types/misc.ts delete mode 100644 u/types/object.ts delete mode 100644 u/types/tagged.ts (limited to 'u/types') diff --git a/u/types/collections/cons.ts b/u/types/collections/cons.ts deleted file mode 100644 index 05dbe7c..0000000 --- a/u/types/collections/cons.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { IOptional, Mapper, Optional } from '@emprespresso/pengueno'; - -export interface ICons extends Iterable { - readonly value: T; - readonly next: IOptional>; - - readonly replace: Mapper>; - readonly before: Mapper>, ICons>; -} - -export class Cons implements ICons { - constructor( - public readonly value: T, - public readonly next: IOptional> = Optional.none(), - ) {} - - public before(head: IOptional>): ICons { - return new Cons(this.value, head); - } - - public replace(_value: T): ICons { - return new Cons(_value, this.next); - } - - *[Symbol.iterator]() { - for (let cur = Optional.some>(this); cur.present(); cur = cur.flatMap((cur) => cur.next)) { - yield cur.get().value; - } - } - - static addOnto(items: Iterable, tail: IOptional>): IOptional> { - return Array.from(items) - .reverse() - .reduce((cons, value) => Optional.from>(new Cons(value, cons)), tail); - } - - static from(items: Iterable): IOptional> { - return Cons.addOnto(items, Optional.none()); - } -} diff --git a/u/types/collections/index.ts b/u/types/collections/index.ts deleted file mode 100644 index 69e5d0b..0000000 --- a/u/types/collections/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './cons.js'; -export * from './list_zipper.js'; diff --git a/u/types/collections/list_zipper.ts b/u/types/collections/list_zipper.ts deleted file mode 100644 index 3df15b5..0000000 --- a/u/types/collections/list_zipper.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { Cons, ICons } from './cons.js'; -import { IOptional, Mapper, Optional, Supplier } from '@emprespresso/pengueno'; - -export interface IZipper extends Iterable { - readonly read: Supplier>; - readonly next: Supplier>>; - readonly previous: Supplier>>; - - readonly prependChunk: Mapper, IZipper>; - readonly prepend: Mapper>; - readonly remove: Supplier>; - readonly replace: Mapper>; -} - -export class ListZipper implements IZipper { - private constructor( - private readonly reversedPathToHead: IOptional>, - private readonly currentHead: IOptional>, - ) {} - - public read(): IOptional { - return this.currentHead.map(({ value }) => value); - } - - public next(): IOptional> { - return this.currentHead.map>( - (head) => new ListZipper(Optional.some(head.before(this.reversedPathToHead)), head.next), - ); - } - - public previous(): IOptional> { - return this.reversedPathToHead.map>( - (lastVisited) => new ListZipper(lastVisited.next, Optional.some(lastVisited.before(this.currentHead))), - ); - } - - public prependChunk(values: Iterable): IZipper { - return new ListZipper(Cons.addOnto(Array.from(values).reverse(), this.reversedPathToHead), this.currentHead); - } - - public prepend(value: T): IZipper { - return this.prependChunk([value]); - } - - public remove(): IZipper { - const newHead = this.currentHead.flatMap((right) => right.next); - return new ListZipper(this.reversedPathToHead, newHead); - } - - public replace(value: T): IZipper { - const newHead = this.currentHead.map((right) => right.replace(value)); - return new ListZipper(this.reversedPathToHead, newHead); - } - - *[Symbol.iterator]() { - let head: ListZipper = this; - for (let prev = head.previous(); prev.present(); prev = prev.flatMap((p) => p.previous())) { - head = >prev.get(); - } - if (head.currentHead.present()) yield* head.currentHead.get(); - } - - public collection() { - return Array.from(this); - } - - static from(iterable: Iterable): ListZipper { - return new ListZipper(Optional.none(), Cons.from(iterable)); - } -} diff --git a/u/types/fn/callable.ts b/u/types/fn/callable.ts deleted file mode 100644 index 60d747b..0000000 --- a/u/types/fn/callable.ts +++ /dev/null @@ -1,21 +0,0 @@ -export interface Callable { - (...args: Array): T; -} - -export interface Supplier extends Callable { - (): T; -} - -export interface Mapper extends Callable { - (t: T): U; -} - -export interface Predicate extends Mapper {} - -export interface BiMapper extends Callable { - (t: T, u: U): R; -} - -export interface SideEffect extends Mapper {} - -export interface BiSideEffect extends BiMapper {} diff --git a/u/types/fn/either.ts b/u/types/fn/either.ts deleted file mode 100644 index 0f65859..0000000 --- a/u/types/fn/either.ts +++ /dev/null @@ -1,143 +0,0 @@ -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 left: Supplier>; - readonly right: Supplier>; - - 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: (a: O, b: T) => _T) => IEither; - readonly joinRightAsync: ( - other: (() => Promise>) | Promise>, - mapper: (a: O, b: T) => _T, - ) => Promise>; -} - -const ELeftTag = 'E.Left' as const; -type ELeftTag = typeof ELeftTag; -export const isLeft = (o: unknown): o is Left => isTagged(o, ELeftTag); -interface Left extends Tagged { - err: E; -} - -const ERightTag = 'E.Right' as const; -type ERightTag = typeof ERightTag; -export const isRight = (o: unknown): o is Right => isTagged(o, ERightTag); -interface Right extends Tagged { - ok: T; -} - -class _Tagged implements Tagged { - protected constructor(public readonly _tag = IEitherTag) {} -} - -export class Either extends _Tagged implements IEither { - protected constructor(private readonly self: Left | Right) { - super(); - } - - public moveRight<_T>(t: _T) { - return this.mapRight(() => t); - } - - public mapBoth<_E, _T>(errBranch: Mapper, okBranch: Mapper): IEither<_E, _T> { - if (isLeft(this.self)) return Either.left(errBranch(this.self.err)); - return Either.right(okBranch(this.self.ok)); - } - - public mapRight<_T>(mapper: Mapper): IEither { - if (isRight(this.self)) return Either.right(mapper(this.self.ok)); - return Either.left(this.self.err); - } - - public mapLeft<_E>(mapper: Mapper): IEither<_E, T> { - if (isLeft(this.self)) return Either.left(mapper(this.self.err)); - return Either.right(this.self.ok); - } - - public flatMap<_T>(mapper: Mapper>): IEither { - if (isRight(this.self)) return mapper(this.self.ok); - 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)); - } - - public fold<_T>(leftFolder: Mapper, rightFolder: Mapper): _T { - if (isLeft(this.self)) return leftFolder(this.self.err); - return rightFolder(this.self.ok); - } - - public left(): IOptional { - if (isLeft(this.self)) return Optional.from(this.self.err) as IOptional; - return Optional.none(); - } - - public right(): IOptional { - if (isRight(this.self)) return Optional.from(this.self.ok) as IOptional; - return Optional.none(); - } - - public joinRight(other: IEither, mapper: BiMapper) { - return this.flatMap((t) => other.mapRight((o) => mapper(o, t))); - } - - public joinRightAsync( - other: Supplier>> | Promise>, - mapper: BiMapper, - ) { - return this.flatMapAsync(async (t) => { - const o = typeof other === 'function' ? other() : other; - return await o.then((other) => other.mapRight((o) => mapper(o, t))); - }); - } - - static left(e: E): IEither { - return new Either({ err: e, _tag: ELeftTag }); - } - - static right(t: T): IEither { - return new Either({ ok: t, _tag: ERightTag }); - } - - static fromFailable(s: Supplier): IEither { - try { - return Either.right(s()); - } catch (e) { - return Either.left(e as E); - } - } - - static async fromFailableAsync(s: Supplier> | Promise): Promise> { - return await (typeof s === 'function' ? s() : s) - .then((t: T) => Either.right(t)) - .catch((e: E) => Either.left(e)); - } -} diff --git a/u/types/fn/index.ts b/u/types/fn/index.ts deleted file mode 100644 index 780c86c..0000000 --- a/u/types/fn/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './callable.js'; -export * from './either.js'; -export * from './optional.js'; diff --git a/u/types/fn/optional.ts b/u/types/fn/optional.ts deleted file mode 100644 index 504e496..0000000 --- a/u/types/fn/optional.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { type Mapper, Predicate, type Supplier, Tagged, isTagged } from '@emprespresso/pengueno'; - -export type MaybeGiven = T | undefined | null; - -export const IOptionalTag = 'IOptional' as const; -export type IOptionalTag = typeof IOptionalTag; -export const isOptional = (o: unknown): o is IOptional => isTagged(o, IOptionalTag); -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: Predicate) => IOptional; - readonly flatMap: <_T>(mapper: Mapper>>) => IOptional<_T>; - readonly orSome: (supplier: Supplier>) => IOptional; - readonly get: Supplier; - readonly present: Supplier; -} - -type OSomeTag = typeof OSomeTag; -const OSomeTag = 'O.Some' as const; -interface Some extends Tagged { - value: NonNullable; -} - -const ONoneTag = 'O.None' as const; -type ONoneTag = typeof ONoneTag; -interface None extends Tagged {} - -const isNone = (o: unknown): o is None => isTagged(o, ONoneTag); -const isSome = (o: unknown): o is Some => isTagged(o, OSomeTag); - -class _Tagged implements Tagged { - protected constructor(public readonly _tag = IOptionalTag) {} -} - -export class Optional = NonNullable> extends _Tagged implements IOptional { - private constructor(private readonly self: Some | None) { - super(); - } - - public move<_T>(t: MaybeGiven<_T>): IOptional<_T> { - return this.map(() => t); - } - - public orSome(supplier: Supplier>): IOptional { - if (isNone(this.self)) return Optional.from(supplier()); - return this; - } - - public get(): T { - if (isNone(this.self)) throw new IOptionalEmptyError('called get() on None optional'); - return this.self.value; - } - - public filter(mapper: Predicate): IOptional { - if (isNone(this.self) || !mapper(this.self.value)) return Optional.none(); - return Optional.some(this.self.value); - } - - public map<_T>(mapper: Mapper>): IOptional<_T> { - if (isNone(this.self)) return Optional.none(); - return Optional.from(mapper(this.self.value)) as IOptional<_T>; - } - - public flatMap<_T>(mapper: Mapper>>): IOptional<_T> { - if (isNone(this.self)) return Optional.none(); - return Optional.from(mapper(this.self.value)) - .orSome(() => Optional.none()) - .get(); - } - - public present() { - return isSome(this.self); - } - - *[Symbol.iterator]() { - if (isSome(this.self)) yield this.self.value; - } - - static some = NonNullable>(value: T): IOptional { - return new Optional({ value, _tag: OSomeTag }); - } - - private static readonly _none = new Optional({ _tag: ONoneTag }); - static none(): IOptional { - return this._none as unknown as IOptional; - } - - static from = NonNullable>(value: MaybeGiven): IOptional { - if (value === null || value === undefined) return Optional.none(); - return Optional.some(value); - } -} diff --git a/u/types/index.ts b/u/types/index.ts deleted file mode 100644 index fc1b15a..0000000 --- a/u/types/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -export * from './misc.js'; - -export * from './object.js'; -export * from './tagged.js'; - -export * from './fn/index.js'; -export * from './collections/index.js'; diff --git a/u/types/misc.ts b/u/types/misc.ts deleted file mode 100644 index 77833c4..0000000 --- a/u/types/misc.ts +++ /dev/null @@ -1,3 +0,0 @@ -export type ObjectFromList, V = string> = { - [K in T extends ReadonlyArray ? U : never]: V; -}; diff --git a/u/types/object.ts b/u/types/object.ts deleted file mode 100644 index fe97999..0000000 --- a/u/types/object.ts +++ /dev/null @@ -1 +0,0 @@ -export const isObject = (o: unknown): o is object => typeof o === 'object' && !Array.isArray(o) && !!o; diff --git a/u/types/tagged.ts b/u/types/tagged.ts deleted file mode 100644 index 351e4c9..0000000 --- a/u/types/tagged.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { isObject } from './index.js'; - -export interface Tagged { - _tag: TTag; -} - -export const isTagged = (o: unknown, tag: TTag): o is Tagged => - !!(isObject(o) && '_tag' in o && o._tag === tag); -- cgit v1.2.3-70-g09d2