From 58be1809c46cbe517a18d86d0af52179dcc5cbf6 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 29 Jun 2025 17:31:30 -0700 Subject: Move to nodejs and also lots of significant refactoring that should've been broken up but idgaf --- u/types/collections/cons.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 u/types/collections/cons.ts (limited to 'u/types/collections/cons.ts') diff --git a/u/types/collections/cons.ts b/u/types/collections/cons.ts new file mode 100644 index 0000000..05dbe7c --- /dev/null +++ b/u/types/collections/cons.ts @@ -0,0 +1,40 @@ +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()); + } +} -- cgit v1.2.3-70-g09d2