Function currification refers to calling a function with only a few arguments and having it return a function to process the rest.

Such as:

const sum = (a, b, c) = > a + b + c

const curried = curry(sum)
console.log(curried(1) (2) (3)) / / 6
console.log(curried(1.3) (5)) / / 9
console.log(curried(2) (4) (6)) / / 12
Copy the code

Implementation flowchart:

Implementation code:

function curry(fn, ... args) {
  const argNum = fn.length // The number of function arguments
  return fillArgs(fn, argNum, args || [])
}

// Get parameters
function fillArgs(fn, argNum, currArgs) {
  if(argNum === currArgs.length) {
    returnfn(... currArgs) }return (. args) = > {
    return fillArgs(fn, argNum, [...currArgs, ...args])
  }
}
Copy the code