rklaehn / adt-ts Goto Github PK
View Code? Open in Web Editor NEWType-safe algebraic data types and pattern matching in typescript 2.8
Type-safe algebraic data types and pattern matching in typescript 2.8
This a great library for ADTs in TypeScript, actually it's the only one I found that actually works well even for recursive types. Right now I'm using it like this:
export type Term = FromCases<{
Var: { tag: 'Var', index: number };
Global: { name: Name },
Abs: { body: Term },
App: { left: Term, right: Term },
Pair: { fst: Term, snd: Term },
Proj: { proj: 'fst' | 'snd', term: Term },
Let: { val: Term, body: Term },
}>;
export const TermC = Constructor<Term>();
export const Var = (index: number): Term => TermC.Var({ index });
export const Global = (name: Name): Term => TermC.Global({ name });
export const App = (left: Term, right: Term): Term => TermC.App({ left, right });
export const Abs = (body: Term): Term => TermC.Abs({ body });
export const Pair = (fst: Term, snd: Term): Term => TermC.Pair({ fst, snd });
export const Proj = (proj: 'fst' | 'snd', term: Term): Term => TermC.Proj({ proj, term });
export const Let = (val: Term, body: Term): Term => TermC.Let({ val, body });
The only annoyance I have is that I have to write out the constructors explicitly because I'd like the arguments to not be in an object. Do you think it's technically possible in TypeScript to automatically generate the "tupled" constructors as well?
Side note: the use of Proxy
seems to slow things down a lot, if I manually write constructors I get a 2x speedup compared to using the constructors from Constructor
. Maybe it's possible to write a version of Constructor
where the user has to manually provide that tags so that Proxy
is not needed. Maybe something like:
export const ConstructorFast = <T extends Tagged>(o: { [K in T['type']]: {} }): Constructor<T> => {
const n: any = {};
for (const k in o) n[k] = (args: any) => ({ type: k, ...args });
return n;
};
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.