当前位置:网站首页>js currying
js currying
2022-07-30 09:08:00 【Li Jianyi】
柯里化
概念
柯里化(currying)又称部分求值,is a technique for converting a multi-parameter function to a one-parameter function.
Currying is the way to go f(a, b, c) The functional form of is changed to f(a)(b)的形式.柯里化不会调用函数,Just transform the function.
原理
A curried function of a function,is to pass in a function as a parameter“柯里化函数”,The function receives the incoming function parameters inside the function,After receiving the function does not evaluate,而是继续返回另一个函数,Save the parameters just passed in as a closure,After all parameters have been received,trigger evaluation.
效果
/** * 柯里化函数 * @param {function} f 需要柯里化的函数 * */
function curry(f) {
return fucntion(a) {
return function(b) {
return function(c) {
f(a, b, c);
}
}
}
};
function sum(a, b, c) {
return a + b + c;
}
let currySum = curry(sum);
console.log(currySum(1, 2, 3));
高级柯里化
function curry(f) {
return function curried(...args) {
if(args.length >= f.length) {
return f.apply(this, args);
}else {
return function(...args1) {
return curried.apply(this, args.concat(args2));
}
}
}
}
// 使用方式
function sum(a, b, c) {
return a + b + c;
}
let currySum = curry(sum);
console.log(currySum(1, 2, 3));
console.log(currySum(1, 2)(3));
console.log(currySum(1)(2)(3));
边栏推荐
猜你喜欢
随机推荐
Thinking about digital transformation of construction enterprises in 2022, the road to digital transformation of construction enterprises
基于JSP实现校园二手交易平台
The difference between typescript3-ts and js
elk报错:[syslogs] index has exceeded [1000000]
剖析SGI STL空间配置器(allocate内存分配函数)
test3
typescript3-ts对比js的差别
孙洪鹤讲教材:原点+众筹+产品,逆向营销实战操作方案
如何组装一个注册中心
【Flask框架①】——Flask介绍
如何使用 Jmeter 进行抢购、秒杀等场景下,进行高并发?
ipset restore命令维护set,但原已存在的条目未删除掉
Burpsuite几种爆破方式
hcip第八天
SQL injection vulnerability (postgresql injection)
联想笔记本 如何更改Win10系统开机logo图标
SQL row-column conversion
typescript7-typescript常用类型
阿里云国际版云服务器防火墙设置
Dynamic Lead Time Promising