purelogic-ts
Version:
PureLogic for TypeScript
172 lines (171 loc) • 6.06 kB
TypeScript
import "tslib";
import * as iterable from "iterable-ts";
export declare class CacheMap<T> {
private readonly _map;
set(id: string, value: T): void;
get<X extends T>(id: string, create: () => X): X;
}
/**
* Bag type and related functions.
*/
export declare namespace bag {
class FlatMap<T, I> {
readonly input: Bag<I>;
readonly func: iterable.FlatMapFunc<I, T>;
constructor(input: Bag<I>, func: iterable.FlatMapFunc<I, T>);
}
class DisjointUnion<T> {
readonly a: Bag<T>;
readonly b: Bag<T>;
constructor(a: Bag<T>, b: Bag<T>);
}
class GroupBy<T> {
readonly input: Bag<T>;
readonly toKey: iterable.KeyFunc<T>;
readonly reduce: iterable.ReduceFunc<T>;
constructor(input: Bag<T>, toKey: iterable.KeyFunc<T>, reduce: iterable.ReduceFunc<T>);
}
class Product<T, A, B> {
readonly a: Bag<A>;
readonly b: Bag<B>;
readonly func: iterable.ProductFunc<A, B, T>;
constructor(a: Bag<A>, b: Bag<B>, func: iterable.ProductFunc<A, B, T>);
}
interface Visitor<T, R> {
/**
* LINQ: SelectMany
*/
flatMap<I>(value: FlatMap<T, I>): R;
disjointUnion(value: DisjointUnion<T>): R;
one(value: T): R;
input(): R;
/**
* LINQ: GroupBy
*/
groupBy(value: GroupBy<T>): R;
product<A, B>(value: Product<T, A, B>): R;
}
type Implementation<T> = <R>(visitor: Visitor<T, R>) => R;
class Dif<T> {
readonly value: T;
readonly a: number;
readonly b: number;
constructor(value: T, a: number, b: number);
}
function one<T>(value: T): Bag<T>;
function input<T>(): Bag<T>;
function range(a: number, b: number): Bag<number>;
class Join<A, B> {
readonly key: string;
readonly a: A | undefined;
readonly b: B | undefined;
constructor(key: string, a: A | undefined, b: B | undefined);
}
class Bag<T> {
readonly implementation: Implementation<T>;
readonly id: string;
constructor(implementation: Implementation<T>);
/**
* LINQ: SelectMany
*/
flatMap<O>(func: iterable.FlatMapFunc<T, O>): Bag<O>;
disjointUnion(b: Bag<T>): Bag<T>;
/**
* LINQ: GroupBy
*/
groupBy(toKey: iterable.KeyFunc<T>, reduce: iterable.ReduceFunc<T>): Bag<T>;
product<B, O>(b: Bag<B>, func: iterable.ProductFunc<T, B, O>): Bag<O>;
/**
* LINQ: Select
*/
map<O>(func: iterable.MapFunc<T, O>): Bag<O>;
/**
* LINQ: Where
*/
filter(func: iterable.FilterFunc<T>): Bag<T>;
compact(): Bag<T>;
/**
* LINQ: Accumulate
*/
reduce(func: iterable.ReduceFunc<T>): Bag<T>;
dif(b: Bag<T>): Bag<Dif<T>>;
join<B>(b: Bag<B>, keyT: iterable.KeyFunc<T>, keyB: iterable.KeyFunc<B>, reduceT: iterable.ReduceFunc<T>, reduceB: iterable.ReduceFunc<B>): Bag<Join<T, B>>;
}
}
/**
* Optimized graph.
*/
export declare namespace optimized {
interface NodeVisitor<T, R> {
input(): R;
one(value: T): R;
groupBy(inputs: Bag<T>, toKey: iterable.KeyFunc<T>, reduce: iterable.ReduceFunc<T>): R;
product<A, B>(a: Bag<A>, b: Bag<B>, func: iterable.ProductFunc<A, B, T>): R;
}
type NodeImplementation<T> = <R>(visitor: NodeVisitor<T, R>) => R;
class Node<T> {
readonly id: string;
readonly implementation: NodeImplementation<T>;
constructor(id: string, implementation: NodeImplementation<T>);
link<O>(func: iterable.FlatMapFunc<T, O>): Link<O>;
bag(): Bag<T>;
}
class LinkValue<T, I> {
readonly node: Node<I>;
readonly func: iterable.FlatMapFunc<I, T>;
constructor(node: Node<I>, func: iterable.FlatMapFunc<I, T>);
}
type LinkVisitor<T, R> = <I>(value: LinkValue<T, I>) => R;
type LinkImplementation<T> = <R>(visitor: LinkVisitor<T, R>) => R;
class Link<T> {
readonly implementation: LinkImplementation<T>;
constructor(implementation: LinkImplementation<T>);
nodeId(): string;
flatMap<O>(func: iterable.FlatMapFunc<T, O>): Link<O>;
addFunc(getFunc: <I>() => iterable.FlatMapFunc<I, T>): Link<T>;
}
class Bag<T> {
readonly id: string;
readonly links: Link<T>[];
constructor(id: string, links: Link<T>[]);
linksMap<R>(visitor: LinkVisitor<T, R>): R[];
groupBy(id: string, toKey: iterable.KeyFunc<T>, reduce: iterable.ReduceFunc<T>): Bag<T>;
product<B, O>(id: string, b: Bag<B>, func: iterable.ProductFunc<T, B, O>): Bag<O>;
flatMap<O>(id: string, func: iterable.FlatMapFunc<T, O>): Bag<O>;
disjointUnion(id: string, b: Bag<T>): Bag<T>;
}
function input<T>(id: string): Bag<T>;
function one<T>(id: string, value: T): Bag<T>;
/**
* DAG
*/
class Dag {
private readonly _map;
get<T>(b: bag.Bag<T>): Bag<T>;
}
}
export declare class InputError implements Error {
readonly bagId: string;
readonly name: string;
readonly message: string;
constructor(bagId: string);
}
export declare abstract class Mem<T> {
protected readonly _map: CacheMap<T>;
protected readonly _dag: optimized.Dag;
}
/**
* Synchronous memory back-end.
*/
export declare class SyncMem extends Mem<iterable.I<any>> {
set<T>(input: bag.Bag<T>, factory: iterable.I<T>): void;
get<T>(b: bag.Bag<T>): iterable.I<T>;
private _get<T>(o);
private _fromNode<T>(n);
}
export declare class AsyncMem extends Mem<Promise<iterable.I<any>>> {
set<T>(input: bag.Bag<T>, getArray: Promise<iterable.I<T>>): void;
get<T>(b: bag.Bag<T>): Promise<iterable.I<T>>;
private _get<T>(o);
private _fromNode<T>(n);
}