函数柯里化(Currying)

函数柯里化(Currying)

柯里化通常也称部分求值,其含义是给函数分步传递参数,每次传递参数后部分应用参数,并返回一个更具体的函数接受剩下的参数,这中间可嵌套多层这样的接受部分参数函数,直至返回最后结果。

这样做有什么好处 ?
  • 参数复用:减少重复传递不变的部分参数
  • 延迟计算:节流 防抖
  • 提前确认
参数复用实例
//普通封装

function check(reg, txt) {
  return reg.test(txt);
}
console.log(check(/\d+/g, "test1"));
// =>true
console.log(check(/\d+/g, "testtest"));
// =>false
//柯里化封装

function curryingCheck(reg) {
  return function (txt) {
    return reg.test(txt);
  };
}

let hasNumber = curryingCheck(/\d+/g);
let hasLetter = curryingCheck(/[a-z]+/g);

//校验
console.log(hasNumber("test1"));
// =>true
console.log(hasNumber("testtest"));
// =>false
console.log(hasLetter("21212"));
// =>false
//校验
console.log(curryingCheck(/\d+/g)("asda1"));
// =>true
console.log(curryingCheck(/\d+/g)("asda"));
// =>false

参数复用,利用的事闭包原理,让前面传输过来的参数不要被释放掉。

参数复用
/**
 * 延迟计算
 */
const add = (...args) => args.reduce((a, b) => a + b);

function curryingSum(func) {
  const args = [];
  return function result(...rest) {
    if (rest.length === 0) {
      return func(...args);
    } else {
      args.push(...rest);
      return result;
    }
  };
}

这个函数当参数为空的时候执行了内部参数所有值的相加,当参数不为空的时候将缓存起来,
在为空的时候再相加,同样是用闭包的方式来实现。

提前确认

这一特性经常是用来对浏览器的兼容性做出一些判断并初始化api,比如说我们目前用来监听事件大部分情况是使用addEventListener来实现的,但是一些较久的浏览器并不支持该方法,所以在使用之前,我们可以先做一次判断,之后便可以省略这个步骤了。

function addEvent(type, el, fn, capture = false) {
   if (window.addEventListener) {
     el.addEventListener(type, fn, capture);
   } else if (window.attachEvent) {
     el.attachEvent("on" + type, fn);
   }
 }

通用的封装方法
function curryingFun(fn, length) {
  //第一次调用获取函数 fn 参数的长度,后续调用获取 fn 剩余参数的长度
  length = length || fn.length;
  // curryingFun 包裹之后返回一个新函数,接收参数为 …args
  return function (...args) {
    // 新函数接收的参数长度是否大于等于 fn 剩余参数需要接收的长度
    return args.length >= length
      ? // 满足要求,执行 fn 函数,传入新函数的参数
        fn.apply(this, args)
      : // 不满足要求,递归 curryingFun 函数,新的 fn 为 bind 返回的新函数(bind 绑定了…args 参数,未执行),新的 length 为 fn 剩余参数的长度
        curryingFun(fn.bind(this, ...args), length - args.length);
  };
}

ES6写法

const curryingFun = (fn, arr = []) => (...args) =>
  ((arg) => (arg.length === fn.length ? fn(...arg) : curryingFun(fn, arg)))([
    ...arr,
    ...args,
  ]);

检测一下

const fn = curryingFun(function (a, b, c) {
  console.log([a, b, c]);
});
fn("a", "b", "c"); // ["a", "b", "c"]
const fn2 = curryingFun(function (a, b, c) {
  console.log(a + b + c);
});
fn2(1)(2)(3); .//6

关于柯里化性能方面
  • 存取arguments对象通常要比存取命名参数要慢一点
  • 一些老版本的浏览器在arguments.length的实现上是相当慢的
  • 使用fn.apply( … ) 和 fn.call( … )通常比直接调用fn( … ) 稍微慢点
  • 创建大量嵌套作用域和闭包函数会带来花销,无论是在内存还是速度上

关于柯里化的意义

把函数完全变成「接受一个参数;返回一个值」的固定形式,利用闭包的特性,去除多余参数传递,提高可读性以及代码质量。


参考文档
  • 深入高阶函数应用之柯里化<木易杨> https://www.imooc.com/article/291693

  • 详解JS函数柯里化 flowsands https://www.jianshu.com/p/2975c25e4d71

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值