Skip to content

Instantly share code, notes, and snippets.

@npatmaja
Last active August 21, 2016 16:26
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 npatmaja/022506c01d1d0501a2679abb151ef4f2 to your computer and use it in GitHub Desktop.
Save npatmaja/022506c01d1d0501a2679abb151ef4f2 to your computer and use it in GitHub Desktop.
Functional JS
// Partial application
const sum = (a, b, c) => a + b + c;
const papply = (fn, ...rest) => (...last) => fn.apply(null, [...rest, ...last]);
const a = papply(sum, 5);
console.log(a(2, 3));
// currying
const curry = (fn, ...rest) => {
const ln = fn.length;
const fnArgs = [...rest];
const accumulator = () => {
if ((ln - fnArgs.length) <= 0) {
return fn.apply(null, fnArgs);
}
return (x) => {
if (x) {
fnArgs.push(x);
}
return accumulator();
}
}
return accumulator();
}
const sum = (a, b, c) => a + b + c;
const add5 = curry(sum, 2, 3);
console.log(add5()(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment