Function corrification is the technique of converting a function that takes multiple arguments into a function that takes a single argument (the first argument of the original function) and returns a new function that takes the remaining arguments and returns the result.
The main functions of function Currization:
- Parameters of reuse
- Early return – Returns a new function that takes the remaining arguments and returns the result
- Deferred execution – Returns a new function and waits for execution
const curry = (fn, ... args) = > {
// 2. Check whether the number of parameters passed matches the required parameters of the function to be executed. If not, continue to return curry method, pass the function and parameters to be executed
if (args.length < fn.length) {
Var sum = curry(sumFn); Return to the curry method and pass in the function and arguments to be executed
return (.arguments) = >curry(fn, ... args, ... arguments); }else {
// 3. The function to be executed returns the result as soon as it can be executed
return fn(...args);
}
};
function sumFn(a, b, c) {
return a + b + c;
}
var sum = curry(sumFn);
console.log(sum(2.3) (5)); / / 10
Copy the code