Skip to content

Instantly share code, notes, and snippets.

@rsolci
Created April 30, 2018 13:55
Show Gist options
  • Save rsolci/99fce77cb329798f5d88928b824396a3 to your computer and use it in GitHub Desktop.
Save rsolci/99fce77cb329798f5d88928b824396a3 to your computer and use it in GitHub Desktop.
A simple example of curried function
const multiply = (...params) => {
if (params.length === 3) {
return params[0] * params[1] * params[2];
} else {
return (...moreParams) => multiply(...[...params, ...moreParams]);
}
};
const curry = fun => {
const parameterCount = fun.length;
return (...params) => {
if (params.length === parameterCount) {
return fun(...params);
}
return (...moreParams) => curry(fun)(...params, ...moreParams);
};
};
const multiplyCurried = curry((a, b, c) => a * b * c);
console.log(multiply(2, 3, 4));
console.log(multiply(2)(3)(4));
console.log(multiplyCurried(2, 3, 4));
console.log(multiplyCurried(2)(3)(4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment