diff options
author | Elizabeth Hunt <me@liz.coffee> | 2025-06-29 17:31:30 -0700 |
---|---|---|
committer | Elizabeth Hunt <me@liz.coffee> | 2025-06-29 17:31:30 -0700 |
commit | 58be1809c46cbe517a18d86d0af52179dcc5cbf6 (patch) | |
tree | 9ccc678b3fd48c1a52fe501600dd2c2051740a55 /u/types/collections/cons.ts | |
parent | d4791f3d357634daf506fb8f91cc5332a794c421 (diff) | |
download | ci-58be1809c46cbe517a18d86d0af52179dcc5cbf6.tar.gz ci-58be1809c46cbe517a18d86d0af52179dcc5cbf6.zip |
Move to nodejs and also lots of significant refactoring that should've been broken up but idgaf
Diffstat (limited to 'u/types/collections/cons.ts')
-rw-r--r-- | u/types/collections/cons.ts | 40 |
1 files changed, 40 insertions, 0 deletions
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<T> extends Iterable<T> { + readonly value: T; + readonly next: IOptional<ICons<T>>; + + readonly replace: Mapper<T, ICons<T>>; + readonly before: Mapper<IOptional<ICons<T>>, ICons<T>>; +} + +export class Cons<T> implements ICons<T> { + constructor( + public readonly value: T, + public readonly next: IOptional<ICons<T>> = Optional.none(), + ) {} + + public before(head: IOptional<ICons<T>>): ICons<T> { + return new Cons<T>(this.value, head); + } + + public replace(_value: T): ICons<T> { + return new Cons<T>(_value, this.next); + } + + *[Symbol.iterator]() { + for (let cur = Optional.some<ICons<T>>(this); cur.present(); cur = cur.flatMap((cur) => cur.next)) { + yield cur.get().value; + } + } + + static addOnto<T>(items: Iterable<T>, tail: IOptional<ICons<T>>): IOptional<ICons<T>> { + return Array.from(items) + .reverse() + .reduce((cons, value) => Optional.from<ICons<T>>(new Cons<T>(value, cons)), tail); + } + + static from<T>(items: Iterable<T>): IOptional<ICons<T>> { + return Cons.addOnto(items, Optional.none()); + } +} |