网站开发容易吗,商业空间设计案例网站,宁波市建设工程监理协会网站,增城做网站要多少钱柯里化
定义
首先#xff0c;我们参照维基百科给出的定义#xff1a; 在计算机科学中#xff0c;柯里化#xff08;英语#xff1a;Currying#xff09;#xff0c;又译为卡瑞化或加里化#xff0c;是把接受多个参数的函数变换成接受一个单一参数#xff08;最初函数… 柯里化
定义
首先我们参照维基百科给出的定义 在计算机科学中柯里化英语Currying又译为卡瑞化或加里化是把接受多个参数的函数变换成接受一个单一参数最初函数的第一个参数的函数并且返回接受余下的参数而且返回结果的新函数的技术 例如
简单写个sum函数
const sum function(a,b) {return a b;
}// 调用
sum(1,2)柯西化这个sum函数
const sum function(a) {return function(b) {return a b;}
}
// 调用
sum(1)(2)实现
那如果有N个参数呢以及我要定一个函数能将所有的函数变成柯里化呢而非仅仅是上面的sum函数
为了解决N个参数的问题所以我们想到了递归函数。
为了解决将所有的函数变成柯里化所以我们的入参要加上fn
function currying(fn, length) {// 第一次调用获取函数 fn 参数的长度后续调用获取 fn 剩余参数的长度length length || fn.length; // currying 包裹之后返回一个新函数接收参数为 ...argsreturn function (...args) { // 新函数接收的参数长度是否大于等于 fn 剩余参数需要接收的长度return args.length length ? fn.apply(this, args) // 满足要求执行 fn 函数传入新函数的参数: currying(fn.bind(this, ...args), length - args.length) // 不满足要求递归 currying 函数新的 fn 为 bind 返回的新函数//bind 绑定了 ...args 参数未执行新的 length 为 fn 剩余参数的长度}}const sum function(a,b,c){return a b c;}const currySum currying(sum);console.log(currySum(1,2,3));
console.log(currySum(1)(2)(3));
console.log(currySum(1,2)(3));
// 输出都是 6 快速使用
lodash中的已经带有了curry函数可以使用使用方式如下
const sum function(a, b, c) {return a b c;
};var currySum _.curry(abc);console.log(currySum(1,2,3));
console.log(currySum(1)(2)(3));
console.log(currySum(1,2)(3));
// 输出都是 6 柯里化的作用
提高自由度
我们可以考虑一个情况如果对于一个函数它有3个参数,它的作用是将3个参数相乘返回。如果我突然想乘用2个数的结果呢是不是使用柯里化就能拿到中间的结果了。
例如bind函数:bind函数也算是提高自由度的例子
const obj {name: 小明,
};const fn function () {console.log(this.name);
}.bind(obj);这里 bind 用来改变函数执行时候的上下文但是函数本身并不执行所以本质上是延迟计算,提高函数的自由度。
我们可以查看一下bind的简单实现返回了函数本身也算是一种柯里化吧。
// 简化实现完整版实现中的第 2 步
Function.prototype.bind function (context) {var self this;// 第 1 个参数是指定的 this截取保存第 1 个之后的参数// arr.slice(begin); 即 [begin, end]var args Array.prototype.slice.call(arguments, 1); return function () {// 此时的 arguments 是指 bind 返回的函数调用时接收的参数// 即 return function 的参数和上面那个不同// 类数组转成数组var bindArgs Array.prototype.slice.call(arguments);// 执行函数return self.apply( context, args.concat(bindArgs) );}
} 文章转载自: http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.bcnsl.cn.gov.cn.bcnsl.cn http://www.morning.sbkb.cn.gov.cn.sbkb.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.jbmsp.cn.gov.cn.jbmsp.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.kwqt.cn.gov.cn.kwqt.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.glxdk.cn.gov.cn.glxdk.cn http://www.morning.hcqpc.cn.gov.cn.hcqpc.cn http://www.morning.tthmg.cn.gov.cn.tthmg.cn http://www.morning.tgmfg.cn.gov.cn.tgmfg.cn http://www.morning.dfbeer.com.gov.cn.dfbeer.com http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.qbkw.cn.gov.cn.qbkw.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.wxfjx.cn.gov.cn.wxfjx.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.qdxtj.cn.gov.cn.qdxtj.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.lynmt.cn.gov.cn.lynmt.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.rpwm.cn.gov.cn.rpwm.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.rsjf.cn.gov.cn.rsjf.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.cflxx.cn.gov.cn.cflxx.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.gthwr.cn.gov.cn.gthwr.cn http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.rpljf.cn.gov.cn.rpljf.cn http://www.morning.drcnn.cn.gov.cn.drcnn.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.cbpkr.cn.gov.cn.cbpkr.cn http://www.morning.hnmbq.cn.gov.cn.hnmbq.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.xlbtz.cn.gov.cn.xlbtz.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.hnmbq.cn.gov.cn.hnmbq.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.dfltx.cn.gov.cn.dfltx.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.pcshb.cn.gov.cn.pcshb.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.sjbty.cn.gov.cn.sjbty.cn http://www.morning.wfspn.cn.gov.cn.wfspn.cn http://www.morning.ntqlz.cn.gov.cn.ntqlz.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn http://www.morning.dmthy.cn.gov.cn.dmthy.cn http://www.morning.rmppf.cn.gov.cn.rmppf.cn http://www.morning.frfnb.cn.gov.cn.frfnb.cn http://www.morning.xrwbc.cn.gov.cn.xrwbc.cn http://www.morning.dgknl.cn.gov.cn.dgknl.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.slmbg.cn.gov.cn.slmbg.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.c7624.cn.gov.cn.c7624.cn http://www.morning.mflqd.cn.gov.cn.mflqd.cn http://www.morning.dfwkn.cn.gov.cn.dfwkn.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.yydeq.cn.gov.cn.yydeq.cn