当前位置:网站首页>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
边栏推荐
- OPENSSL ASN.1, DER, PEM, X509
- window10安装keras
- 计算机视觉(AI)面试大全
- [safety science popularization] have you been accepted by social workers today?
- R生物信息学统计分析
- 正则表达式
- js点击太阳月亮切换白天黑夜js特效
- 亚马逊测评自养号,小白应该如何开始?
- Canvas interactive star animation background JS special effect
- JS click the sun and moon to switch between day and night JS special effect
猜你喜欢

Artalk | how to build a domestic hyperfusion evolutionary base with minimum investment?

【ELT.ZIP】OpenHarmony啃论文俱乐部——多层存储分级数据压缩

Azure Kubernates Service 更新|提升开发体验和效率

Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup

【ELT.ZIP】OpenHarmony啃论文俱乐部——快速随机访问字符串压缩

Unity之数据持久化——Json

JS click the sun and moon to switch between day and night JS special effect

ThoughtWorks.QRCode功能齐全的生成器

OpenGL第七章 基础光照

Free flying animation of paper plane based on SVG
随机推荐
Lombok use
OpenGL第八章 材质material
【安全科普】今天你被社工了吗?
Free flying animation of paper plane based on SVG
WEB上传文件预览
ArTalk | 如何用最小投入,构建国产超融合进化底座?
Windows10 installing keras
OpenGL错误指南
Difference between idea open and import project
Lvgl Chinese font production
postgresql源码学习(十七)—— MVCC②-快照与隔离级别简介
net::ERR_ FILE_ NOT_ Found error
Azure Kubernates Service 更新|提升开发体验和效率
Disk quota exceeded
OPENSSL ASN. 1, DER, PEM, X509
canvas交互式星星动画背景js特效
Canvas interactive star animation background JS special effect
OpenGL第十一章 多光源
Gd32 can sends no mailbox fault
基于SSM框架的学生在线教育教学课程管理系统