当前位置:网站首页>Handwriting Currying - toString Comprehension
Handwriting Currying - toString Comprehension
2022-08-05 09:32:00 【InfoQ】
let arr = []
function addCurry() {
let arg = Array.prototype.slice.call(arguments); // Collect parameters
arr = arr.concat(arg);
if (arg.length === 0) { //If the parameter is empty, judge the end of the recursion
return arr.reduce((a,b)=>{return a+b}) // Summation
} else {
return addCurry;
}
}
addCurry(1)(2)(3)() // 6
Array.prototype.slice.call(arguments)
arr
reduce()
addCurry(1)(2)(3)() // 6
addCurry(1)() // 7
function addCurry() {
let arr = [...arguments]
let fn = function () {
if(arguments.length === 0) {
return arr.reduce((a, b) => a + b)
} else {
arr.push(...arguments)
return fn
}
}
return fn
}
addCurry(1)(2)(3)
//or
addCurry(1)(2,3)
toString
let fn = function(){}
fn.toString = () => 1
fn == 1 // true
function addCurry() {
let arr = [...arguments]
// Use closures to collect all parameter values
varfn = function() {
arr.push(...arguments);
return fn;
};
//Using toString implicit conversion
fn.toString = function () {
return arr.reduce(function (a, b) {
return a + b;
});
}
return fn;
}
addCurry(1)(2)(3) == 6 // true
边栏推荐
- 5. Deploy the web project to the cloud server
- Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
- 无题二
- XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法
- 欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
- Oracle temporary table space role
- tensorflow.keras无法引入layers
- 只有一台交换机,如何实现主从自动切换之nqa
- ffmpeg drawtext 添加文本水印
- Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
猜你喜欢
MySQL advanced (twenty-seven) database index principle
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(上)
After Keil upgrades to AC6, what changes?
【AGC】增长服务1-远程配置示例
蚁剑webshell动态加密连接分析与实践
Oracle临时表空间作用
Pytorch深度学习快速入门教程 -- 土堆教程笔记(三)
Weekly Report 2022-8-4
交换机端口的三种类型详解与hybrid端口实验
Dry goods!Generative Model Evaluation and Diagnosis
随机推荐
Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
2022.8.3
Keil升级到AC6后,到底有哪些变化?
dotnet OpenXML 解析 PPT 图表 面积图入门
哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
The Seven Weapons of Programmers
正则表达式replaceAll()方法具有什么功能呢?
5. Deploy the web project to the cloud server
How to realize the short press and long press detection of the button?
线程之Happens-before规则
【ASM】字节码操作 方法的初始化 Frame
2022/8/4 考试总结
ffmpeg drawtext 添加文本水印
无题十
无题九
【Excel实战】--图表联动demo_001
深度学习21天——卷积神经网络(CNN):服装图像分类(第3天)
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
Two-table query average grouping in sql server
Why do I recommend using smart async?