js柯里化

function sum(a, b, c) {
  return a + b + c
}

function curry(f) {
  return function fn(...args) {
    if (args.length >= f.length) {
      return f.apply(this, args)
    } else {
      return function (...args2) {
        return fn.apply(this, args.concat(args2))
      }
    }
  }
}

const sc = curry(sum)
console.log(sc(1, 2, 3))
console.log(sc(1)(2, 3))
console.log(sc(1, 2)(3))
posted @ 2022-10-17 16:46  Samsara315  阅读(2)  评论(0编辑  收藏  举报