当前位置:网站首页>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)arrreduce()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)
toStringlet 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
边栏推荐
- 欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(下)
- 链表中的数字相加----链表专题
- Tanabata romantic date without overtime, RPA robot helps you get the job done
- 只有一台交换机,如何实现主从自动切换之nqa
- openpyxl操作Excel文件
- Creo 9.0 基准特征:基准平面
- Dry goods!Generative Model Evaluation and Diagnosis
- Weekly Report 2022-8-4
- 哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
- leetcode points to Offer 10- I. Fibonacci sequence
猜你喜欢

【LeetCode】623. Add a row to the binary tree

链表中的数字相加----链表专题

5. Deploy the web project to the cloud server

leetcode: 529. Minesweeper Game

【ASM】字节码操作 方法的初始化 Frame

After Keil upgrades to AC6, what changes?

ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...

shell脚本实例

19.服务器端会话技术Session

蚁剑webshell动态加密连接分析与实践
随机推荐
Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
无题十一
欧盟 | 地平线 2020 ENSEMBLE:D2.13 SOTIF Safety Concept(下)
How to realize the short press and long press detection of the button?
无题七
Redis源码解析:Redis Cluster
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g
手把手教你纯c实现异常捕获try-catch组件
2.4G无线收发模块的应用
无题十三
my journal link
After Keil upgrades to AC6, what changes?
Seata source code analysis: initialization process of TM RM client
如何实现按键的短按、长按检测?
微服务 技术栈
无题一
并发之CAS
无题五
CCVR eases heterogeneous federated learning based on classifier calibration
无题二