3 ms·
Cool to see a post about this sort of thing! I came up with a similar pattern a while ago (using Flow instead of TypeScript) for a lambda calculus evaluator. I
by alangpierce 9y ago
Cool to see a post about this sort of thing! I came up with a similar pattern a while ago (using Flow instead of TypeScript) for a lambda calculus evaluator. I ended up using a code generation approach so I define my datatypes in one file and the `match` method and `Matcher` interface are generated for me.
Here's where I define the five cases for a lambda calculus expression undergoing evaluation:
https://github.com/alangpierce/LambdaCalculusPlayground/blob/master/typeDefs.js#L137 https://github.com/alangpierce/LambdaCalculusPlayground/blob...
Here are the autogenerated types for those cases (there's other code to make it work at runtime):
https://github.com/alangpierce/LambdaCalculusPlayground/blob/master/types.js.flow#L458 https://github.com/alangpierce/LambdaCalculusPlayground/blob...
And here's a simple example of using it:
https://github.com/alangpierce/LambdaCalculusPlayground/blob/master/ExpressionEvaluator.js#L231 https://github.com/alangpierce/LambdaCalculusPlayground/blob...