Last active
November 19, 2017 15:00
-
-
Save buzzdecafe/ebb421638429d603a8d7f94e5dc0e175 to your computer and use it in GitHub Desktop.
decorated curry
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 meta = Symbol('@@ramda-meta'); | |
const initMeta = f => ({ func: f, arity: f.length, seenArgs: [] }); | |
const getMeta = f => f[meta] || initMeta(f); | |
const setMeta = (arg, f) => { | |
const fMeta = getMeta(f); | |
f[meta] = { | |
func: fMeta.func, | |
arity: fMeta.arity - 1, | |
seenArgs: fMeta.seenArgs.concat(arg) | |
}; | |
return f; | |
}; | |
const _curryN = (n, f) => { | |
const fMeta = getMeta(f); | |
const curried = a => { | |
const recurried = setMeta(a, _curryN(fMeta.arity - 1, curried)); | |
return (recurried[meta].arity === 0 | |
? recurried[meta].func.apply(null, recurried[meta].seenArgs) | |
: recurried | |
); | |
}; | |
curried[meta] = fMeta; | |
return curried; | |
}; |
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
// abc :: (String, String, String) -> String | |
function abc(a, b, c) { | |
return '' + a + b + c; | |
} | |
const cabc = _curryN(3, abc); // cabc :: String -> String -> String -> String | |
const gotA = cabc('A'); // gotA :: String -> String -> String | |
const gotAB = gotA('-B-'); // gotB :: String -> String | |
const gotABC = gotAB('C!'); // gotABC :: String 'A-B-C!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment