From 9970036d203ba2d0a46b35ba6fad21d49441cdd4 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 27 Jul 2025 17:03:10 -0700 Subject: hai --- lib/trace/itrace.ts | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 lib/trace/itrace.ts (limited to 'lib/trace/itrace.ts') diff --git a/lib/trace/itrace.ts b/lib/trace/itrace.ts new file mode 100644 index 0000000..e2019fa --- /dev/null +++ b/lib/trace/itrace.ts @@ -0,0 +1,91 @@ +import type { Mapper, SideEffect, Supplier } from '@emprespresso/pengueno'; + +/** + * the "thing" every Trace writer must "trace()". + */ +type BaseTraceWith = string; +export type ITraceWith = BaseTraceWith | T; +export interface ITrace { + /** + * creates a new trace scope which inherits from this trace. + */ + traceScope: Mapper, ITrace>; + + /** + * does the tracing. + */ + trace: SideEffect>; +} + +export type ITraceableTuple = { item: T; trace: BaseTraceWith | TraceWith }; +export type ITraceableMapper = (w: ITraceable) => _T; + +export interface ITraceable { + readonly trace: ITrace; + 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>) => Array>; + 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>; +} + +export class TraceableImpl implements ITraceable { + protected constructor( + private readonly item: T, + public readonly trace: ITrace, + ) {} + + public map<_T>(mapper: ITraceableMapper) { + const result = mapper(this); + return new TraceableImpl(result, this.trace); + } + + public coExtend<_T>(mapper: ITraceableMapper, Trace>): Array> { + 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>, Trace>, + ): ITraceable, Trace> { + return new TraceableImpl( + mapper(this).then((t) => t.get()), + this.trace, + ); + } + + 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, Trace> { + return this.map(() => t); + } + + public bimap<_T>(mapper: ITraceableMapper, Trace>) { + const { item, trace: _trace } = mapper(this); + return this.move(item).traceScope(() => _trace); + } + + public get() { + return this.item; + } +} -- cgit v1.2.3-70-g09d2