js Curry 函数科里化

 

 

https://mp.weixin.qq.com/s?__biz=MzAwNjI5MTYyMw==&mid=2651501763&idx=1&sn=cfe7920ad3a966ef995049cff6bfa1fa&chksm=80f1bd0bb786341dd00ccd2baedaafedbc5cdecc846109f278786fc74929d07c51cc08589959&scene=126&sessionid=1678330049#rd

 

Curry 化是一种将多参数函数转换为单参数函数的技术

 

 

function curry(fn){
  return function curried(...args){
    if(args.length >= fn.length){
      return fn.apply(this, args);
    } else {
      return function(...rest){
        return curried.apply(this, args.concat(rest));
      };
    }
  };
}
function add(x, y, z){
  return x + y + z;
}
const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)(3));

 

posted @ 2023-03-09 12:17  AngDH  阅读(17)  评论(0编辑  收藏  举报