Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Created November 7, 2018 22:19
Show Gist options
  • Save abiodun0/853569665f83fa5d540c51e6f5454615 to your computer and use it in GitHub Desktop.
Save abiodun0/853569665f83fa5d540c51e6f5454615 to your computer and use it in GitHub Desktop.
Some Adts
const uncurry = f => args => args.reduce((p, c) => p(c), f)
const curry = n => f => {
const rec = a => acc => (a === 0 ? f(acc) : x => rec(a - 1)([...acc, x]))
return rec(n)([])
}
const mapWithKey = f => o =>
Object.keys(o).reduce((p, k) => ({ ...p, [k]: f(k)(o[k]) }), {})
const match = cases => ({ label, values }) => uncurry(cases[label])(values)
const create = def => {
const createConstructor = label => types =>
curry(types.length)(values => ({ label, values }))
const constructors = mapWithKey(createConstructor)(def)
return { ...constructors, match, def }
}
module.exports = { match, create, adt: create }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment