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/trace/itrace.ts | 75 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 27 deletions(-) (limited to 'u/trace/itrace.ts') diff --git a/u/trace/itrace.ts b/u/trace/itrace.ts index 8cf123a..9c33ad2 100644 --- a/u/trace/itrace.ts +++ b/u/trace/itrace.ts @@ -1,69 +1,90 @@ import type { Mapper, SideEffect, Supplier } from '@emprespresso/pengueno'; -// the "thing" every Trace writer must "trace()" +/** + * the "thing" every Trace writer must "trace()". + */ type BaseTraceWith = string; export type ITraceWith = BaseTraceWith | T; export interface ITrace { - addTrace: Mapper, ITrace>; + /** + * creates a new trace scope which inherits from this trace. + */ + traceScope: Mapper, ITrace>; + + /** + * does the tracing. + */ trace: SideEffect>; } -export type ITraceableTuple = [T, BaseTraceWith | TraceWith]; -export type ITraceableMapper> = (w: W) => _T; +export type ITraceableTuple = { item: T; trace: BaseTraceWith | TraceWith }; +export type ITraceableMapper = (w: ITraceable) => _T; export interface ITraceable { readonly trace: ITrace; - get: Supplier; - move: <_T>(t: _T) => ITraceable<_T, Trace>; - map: <_T>(mapper: ITraceableMapper) => ITraceable<_T, Trace>; - bimap: <_T>(mapper: ITraceableMapper | Trace>, Trace>) => ITraceable<_T, Trace>; - peek: (peek: ITraceableMapper) => ITraceable; - flatMap: <_T>(mapper: ITraceableMapper, Trace>) => ITraceable<_T, Trace>; - flatMapAsync<_T>( + readonly get: Supplier; + + readonly move: <_T>(t: _T) => ITraceable<_T, Trace>; + readonly map: <_T>(mapper: ITraceableMapper) => ITraceable<_T, Trace>; + readonly bimap: <_T>(mapper: ITraceableMapper, Trace>) => ITraceable<_T, Trace>; + readonly coExtend: <_T>( + mapper: ITraceableMapper, Trace>, + ) => ReadonlyArray>; + readonly peek: (peek: ITraceableMapper) => ITraceable; + + readonly traceScope: (mapper: ITraceableMapper) => ITraceable; + + readonly flatMap: <_T>(mapper: ITraceableMapper, Trace>) => ITraceable<_T, Trace>; + readonly flatMapAsync: <_T>( mapper: ITraceableMapper>, Trace>, - ): ITraceable, Trace>; + ) => ITraceable, Trace>; } -export class TraceableImpl implements ITraceable { +export class TraceableImpl implements ITraceable { protected constructor( private readonly item: T, - public readonly trace: ITrace, + public readonly trace: ITrace, ) {} - public map<_T>(mapper: ITraceableMapper) { + public map<_T>(mapper: ITraceableMapper) { const result = mapper(this); return new TraceableImpl(result, this.trace); } - public flatMap<_T>(mapper: ITraceableMapper, TraceWith>): ITraceable<_T, TraceWith> { + public coExtend<_T>(mapper: ITraceableMapper, Trace>): ReadonlyArray> { + const results = mapper(this); + return Array.from(results).map((result) => this.move(result)); + } + + public flatMap<_T>(mapper: ITraceableMapper, Trace>): ITraceable<_T, Trace> { return mapper(this); } public flatMapAsync<_T>( - mapper: ITraceableMapper>, TraceWith>, - ): ITraceable, TraceWith> { + mapper: ITraceableMapper>, Trace>, + ): ITraceable, Trace> { return new TraceableImpl( mapper(this).then((t) => t.get()), this.trace, ); } - public peek(peek: ITraceableMapper) { + public traceScope(mapper: ITraceableMapper): ITraceable { + return new TraceableImpl(this.get(), this.trace.traceScope(mapper(this))); + } + + public peek(peek: ITraceableMapper) { peek(this); return this; } - public move<_T>(t: _T): ITraceable<_T, TraceWith> { + public move<_T>(t: _T): ITraceable<_T, Trace> { return this.map(() => t); } - public bimap<_T>(mapper: ITraceableMapper | TraceWith>, TraceWith>) { - const [item, trace] = mapper(this); - const traces = Array.isArray(trace) ? trace : [trace]; - return new TraceableImpl( - item, - traces.reduce((trace, _trace) => trace.addTrace(_trace), this.trace), - ); + public bimap<_T>(mapper: ITraceableMapper, Trace>) { + const { item, trace: _trace } = mapper(this); + return this.move(item).traceScope(() => _trace); } public get() { -- cgit v1.2.3-70-g09d2