forEachEmbedded

This commit is contained in:
Tony Garnock-Jones 2023-06-16 12:44:22 +02:00
parent 69010737b9
commit ac6a138a00
1 changed files with 30 additions and 0 deletions

View File

@ -43,6 +43,32 @@ export interface FoldMethods<T, R> {
embedded(t: Embedded<T>, k: Fold<T, R>): R;
}
export class VoidFold<T> implements FoldMethods<T, void> {
boolean(b: boolean): void {}
single(f: number): void {}
double(f: number): void {}
integer(i: number): void {}
string(s: string): void {}
bytes(b: Bytes): void {}
symbol(s: symbol): void {}
record(r: Record<Value<T>, Tuple<Value<T>>, T>, k: Fold<T, void>): void {
k(r.label);
r.forEach(k);
}
array(a: Value<T>[], k: Fold<T, void>): void { a.forEach(k); }
set(s: Set<T>, k: Fold<T, void>): void { s.forEach(k); }
dictionary(d: Dictionary<T>, k: Fold<T, void>): void {
d.forEach((value, key) => { k(key); k(value); });
}
annotated(a: Annotated<T>, k: Fold<T, void>): void { k(a.item); a.annotations.forEach(k); }
embedded(_t: Embedded<T>, _k: Fold<T, void>): void {}
}
export class ForEachEmbedded<T> extends VoidFold<T> {
constructor(public readonly f: (t: T, k: Fold<T, void>) => void) { super(); }
embedded(t: Embedded<T>, k: Fold<T, void>): void { this.f(t.embeddedValue, k); }
}
export abstract class ValueFold<T, R = T> implements FoldMethods<T, Value<R>> {
boolean(b: boolean): Value<R> {
return b;
@ -193,3 +219,7 @@ export function mapEmbeddeds<T, R>(
{
return fold(v, new MapFold(f));
}
export function forEachEmbedded<T>(v: Value<T>, f: (t: T, k: Fold<T, void>) => void): void {
return fold(v, new ForEachEmbedded(f));
}