Created
November 7, 2018 22:19
-
-
Save abiodun0/853569665f83fa5d540c51e6f5454615 to your computer and use it in GitHub Desktop.
Some Adts
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 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