Giter Site home page Giter Site logo

adt-ts's People

Contributors

rklaehn avatar wngr avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

wngr

adt-ts's Issues

Tupled constructors

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;
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.