Created
November 13, 2018 05:53
-
-
Save Kirens/e410e4f553f9929fd4933527d9b7e1a7 to your computer and use it in GitHub Desktop.
WIP: Algebraic data types in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const ADT = cls => variations => { | |
| let package = {} | |
| package[cls] = class {} | |
| variations.forEach(([name, ...args]) => | |
| package[cls][name] = function(...args) { | |
| } | |
| package[cls][name].name = name | |
| package[cls][name].prototype = package[cls] | |
| ) | |
| return package[cls] | |
| } | |
| const caseof = cases => data => { | |
| const case = cases.find(([kind]) => data.constructor === kind) | |
| return case && case[1](data) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment