第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

柯里化函數(shù)的實(shí)現(xiàn)

標(biāo)簽:
JavaScript

记录柯里化函数实现的学习过程:

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

如果要实现下面这个方法:

1add(2)(1, 3, 4)(2, 3)(3)(4, 6)(7, 98)() // 133

上面这个函数当参数为空的时候执行了内部参数所有值的相加,所以我们应该考虑当参数不为空的时候将缓存起来,在为空的时候再相加,这样的思路会用闭包的方式来实现。下面是实现方法:

function add () {  // 用来缓存所有的arguments值  
  let args = [].slice.call(arguments);  // 新建currying函数实现柯里化  
  let currying = function () {    // 如果参数为空,那么递归停止,返回执行结果
    if (arguments.length === 0) {      return args.reduce((a, b) => a + b);
    } else {      // 否则将参数保存到args里面,返回currying方法      args.push(...arguments);      return currying
    }      
  }  return currying
}
1add(2)(1, 3, 4)(2, 3)(3)(4, 6)(7, 98)() // 133

上面有需要注意的一点,因为currying函数里面使用arguments,所以currying不能使用箭头函数,箭头函数内部的arguments的用法与箭头函数内部的this差不多,它取的是上一级函数的arguments值。如果想用箭头函数,currying函数可以这样改动:

  let currying = (...rest) => {    // 如果参数为空,那么递归停止,返回执行结果
    if (rest.length === 0) {      return args.reduce((a, b) => a + b);
    } else {      // 否则将参数保存到args里面,返回currying方法      args.push(...rest);      return currying
    }      
  }

我们返回的currying函数还可以使用callee来实现,原理相同,单数严格模式下不能使用:

function add () {  // 用来缓存所有的arguments值  
  let args = [].slice.call(arguments);  // 新建currying函数实现柯里化  
  return function () {    // 如果参数为空,那么递归停止,返回执行结果
    if (arguments.length === 0) {      return args.reduce((a, b) => a + b);
    } else {      // 否则将参数保存到args里面,返回currying方法      args.push(...arguments);      return arguments.callee
    }      
  }
}
add(2)(1, 3, 4)(2, 3)(3)(4, 6)(7, 98)() // 133

对普通函数进行柯里化:

// 柯里化函数的构造方法function curry (fn) {  // 缓存除第一个参数的所有参数
  let args = [].slice.call(arguments, 1);  
  let _fn = function () {    if (arguments.length === 0) {      return fn.apply(this, args)
    } else {
      args.push(...arguments);      return _fn
    }
  }  return _fn
}function add () {  return [].reduce.call(arguments, (a, b) => a + b)
}
console.log(curry(add, 2)(1, 3, 4)(2, 3)(3)(4, 6)(7, 98)()) // 133

举例柯里化函数思想实现的场景:

如减少重复传递的参数

function simpleURL(protocol, domain, path) {    return protocol + "://" + domain + "/" + path;
}

我们使用的时候将会这样:

var myurl = simpleURL('http', 'mysite', 'home.html');var myurl2 = simpleURL('http', 'mysite', 'aboutme.html');

我们可以用柯里化的思想改写:

function curry (fn) {  // 缓存除第一个参数的所有参数
  let args = [].slice.call(arguments, 1);  
  return function () {return fn.apply(this, args.concat(...arguments))
  }
}// 避免每次调用重复传参let myURL1 = curry(simpleURL, 'https', 'mysite');
let res1 = myURL1('home.html');    //console.log(res1);//https://mysite/home.htmllet myURL2 = curry(simpleURL, 'http', 'mysite');
let res2 = myURL2('aboutme.html');    //console.log(res2);//http://mysite/aboutme.html

原文出处:https://www.cnblogs.com/kdcg/p/10192421.html  

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消