当前位置:网站首页>JS to realize coritization
JS to realize coritization
2022-06-11 03:34:00 【Love to eat virgin fruit】
currying
Definition : The method of transforming a multi parameter function into a single parameter function .
- js Implement the corellization of the function free version
Example :curryingadd(2)(1, 3, 4)(2, 3)(3)(4, 6)(7, 98);
function curryingadd() {
let args = Array.from(arguments);
let innerAdd = function () {
let argsinner = Array.from(arguments);
// If there are no internal parameters at this time, that is f(...arg)() Just return the value directly
if (argsinner.length == 0) {
return args.reduce((prev, cur) => {
return prev + cur;
});
} else {
// We use closures here because args It's externally defined So you can accumulate and store variables during recursion
args.push(...arguments);
return innerAdd;
}
};
innerAdd.toString = function () {
return args.reduce((prev, curr) => {
return prev + curr;
}, 0);
};
return innerAdd;
}
let res = curryingadd(2)(1, 3, 4)(2, 3)(3)(4, 6)(7, 98);//133
let res1= curryingadd(2)(1, 3, 4)(2, 3)(3)(4, 6)(7, 98)();
- js Realize the coritization of function version
Such as :curry(add1)(1, 2)(3)
function add1(x, y, z) {
return x + y + z;
}
const curry = (fn, ...args) =>
// The number of function parameters can be directly determined by the number of functions .length Property to access
args.length >= fn.length // This judgment is crucial !!!
// The parameter passed in is greater than or equal to the original function fn The number of parameters , Then execute the function directly
? fn(...args)
/** * The parameter passed in is smaller than the original function fn Number of parameters * Then continue to curry the current function , Returns a that accepts all parameters ( Current and remaining parameters ) Function of */
: (..._args) => curry(fn, ...args, ..._args);
console.log(curry(add1)(1, 2)(3));//6
Challenge Gengwen 100 God ----- the second day
边栏推荐
- Iqoo 8 measured hands-on experience: return of the king, never high profile
- Lvgl Chinese font production
- 618将至!全渠道开售,高价低配的OPPO Reno6能赢吗?
- 音乐正版率关键数据缺失,网易云音乐IPO胜算几何?
- 名不副实的雅迪高端品牌VFLY,为何“不高端”?
- Log4j use
- OpenGl第十章 投光物
- The tide play power is really firepower! The first big screen cinema for young people? Cool open TV Max 86 "sudden attack
- Canvas rotation drawing H5 animation JS effect
- 被“内卷”酸翻的OPPO Reno6
猜你喜欢
随机推荐
突破中国品牌创新技术实力,TCL做对了什么?
Product milestones in May 2022
Shangpinhui mall_ Background homepage of
Artalk | how to build a domestic hyperfusion evolutionary base with minimum investment?
MySQL learning notes: JSON nested array query
Instructor add function_ Enable auto fill_ Instructor modification function
JS click the sun and moon to switch between day and night JS special effect
OpenSSL enc encryption and decryption
【安全科普】挖矿技术,从一个理工男的爱情故事讲起
Why is vfly, a high-end brand of Yadi that does not live up to its name, not high-end?
Right click PowerShell here function add
PostgreSQL source code learning (XX) -- fault recovery ① - transaction log format
名不副实的雅迪高端品牌VFLY,为何“不高端”?
postgresql 捕获函数中的异常
[safety science popularization] mining technology starts from the love story of a man of science and Engineering
SSL库选择
js最常用的排序---手撕js系列
OpenGL第八章 材质material
LaTex环境下在TexStudio中使用minted插入高亮代码
R生物信息学统计分析








