Skip to content

Instantly share code, notes, and snippets.

@npatmaja npatmaja/functional.js
Last active Aug 21, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.