函数柯里化

函数柯里化

什么是函数柯里化

柯里化是编程语言中的一个通用的概念,是指把接收多个参数的函数变换成接收单一参数的函数,嵌套返回直到所有参数都被使用并返回最终结果。
简单描述,就是把一个函数的某些参数先固化,也就是设置默认值,返回一个新的函数,在新函数中继续接收剩余参数,这样调用这个新函数会更简单
比如说将一个函数从可调用的 f(a, b, c) 转换为可调用的 f(a)(b)(c)

举一个简单的例子

function sum(a,b){
  console.log(a+b); 
}
sum(1,2)
// 柯里化
function sum1(a){
  return function sum2(b){
    console.log(a+b)
  }
}
sum1(1)(2)

柯里化的转换

假设有这样一个场景

function sum(a,b,c,d,e){
  console.log(a+b+c+d+e)
}
sum(1,2,3,4,5);
// 柯里化
function sum1(a){
  return function sum2(b){
    return function sum3(c){
      return function sum4(d){
        return function sum5(e){
          console.log(a+b+c+d+e)
        }
      }
    }
  }
}
sum1(1)(2)(3)(4)(5)

多层柯里化的时候代码会不美观,可读性非常差。

function curry(fn, otherArgs) {
  let length = fn.length; // 函数的形参个数
  let args = otherArgs || []; // 剩余参数
  return function () {
    // 定义一个数组专门用来存储所有的参数
    let newArgs = args.concat(Array.prototype.slice.call(arguments));// 将arguments转成数组的形式处理
    // 如果参数个数小于最初的fn.length,则递归调用,继续收集参数
    if (newArgs.length < length) {
      return curry.call(this, fn, newArgs);
    } else {
      // 参数收集完毕,则执行fn
      return fn.apply(this, newArgs);
    }
  }
}

function sumFn(a, b, c) {
  return a + b + c;
}
let sum = curry(sumFn)

console.log(sum(2)(3)(4));
function add (a,b){
  console.log(arguments)
}
/*
  0: 1
  1: 2
*/
// arguments是一个object

slice 并不需要 this 为 array 类型,只需要有 length 属性即可
Array.prototype.slice.call(arguments)
此时我们将对象this由Array.prototype指向arguments。这样arguments即可使用数组的方法slice

[slice源码地址]https://github.com/v8/v8/blob/ad82a40509c5b5b4680d4299c8f08d6c6d31af3c/src/js/array.js
第587行

// slice的大致写法
function slice(start, end) {
  var len = ToUint32(this.length), result = [];
  for(var i = start; i < end; i++) {
    result.push(this[i]); } return result;
  }
}

柯里化的作用

柯里化大多是情况下是为了减少重复传递的不变参数

function spliceUrl(protocol,hostname,patchname){
  return `${protocol}${hostname}${patchname}`;
}
spliceUrl('https://', 'manage.ne-soft.com/', '123')
// 柯里化
function spliceUrl1(protocol) {
  return function spliceUrl2(hostname) {
    return function spliceUrl3(patchname) {
      return `${protocol}${hostname}${patchname}`;
    }
  }
}
const urlBase = spliceUrl1('https://');
const url1 = urlBase('manage-pre.ne-soft.com/')('123')
const url2 = urlBase('manage-dev.ne-soft.com/')('123')
function spliceUrl1(protocol) {
  return function spliceUrl2(patchname) {
    return function spliceUrl3(hostname) {
      return `${protocol}${hostname}${patchname}`;
    }
  }
}
const urlBase = spliceUrl1('https://')('123');
const url1 = urlBase('manage-pre.ne-soft.com/')
const url2 = urlBase('manage-dev.ne-soft.com/')

函数接收的参数单一,功能也单一和明确,这样更便于在表达式运算中有针对性的调用。

function wrap(tag) {
  let stag = '<' + tag + '>';
  let etag = '</' + tag.replace(/s.*/, '') + '>';
  return function(x) {
    return stag + x + etag;
  }
}

let b = wrap('b');
document.write(b('粗体字'));
let i = wrap('i');
document.write(i('斜体字'));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值