Skip to content

Instantly share code, notes, and snippets.

@Kirens
Created November 13, 2018 05:53
Show Gist options
  • Select an option

  • Save Kirens/e410e4f553f9929fd4933527d9b7e1a7 to your computer and use it in GitHub Desktop.

Select an option

Save Kirens/e410e4f553f9929fd4933527d9b7e1a7 to your computer and use it in GitHub Desktop.
WIP: Algebraic data types in JS
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