Skip to content

Instantly share code, notes, and snippets.

@buzzdecafe
Last active November 19, 2017 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buzzdecafe/ebb421638429d603a8d7f94e5dc0e175 to your computer and use it in GitHub Desktop.
Save buzzdecafe/ebb421638429d603a8d7f94e5dc0e175 to your computer and use it in GitHub Desktop.
decorated curry
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;
};
// 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