当前位置:网站首页>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
边栏推荐
猜你喜欢

seata源码解析:事务状态及全局锁的存储

如何实现按键的短按、长按检测?

偏向锁/轻量锁/重级锁锁锁更健康,上锁解锁到底是怎么完成实现的

蚁剑webshell动态加密连接分析与实践

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

Pytorch Deep Learning Quick Start Tutorial -- Mound Tutorial Notes (3)

哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?

5.部署web项目到云服务器

Hundred lines of code launch red hearts, why programmers lose their girlfriends!

ECCV 2022 Oral Video Instance Segmentation New SOTA: SeqFormer & IDOL and CVPR 2022 Video Instance Segmentation Competition Champion Scheme...
随机推荐
Assembly language (8) x86 inline assembly
Advanced usage of C language
PAT Grade B-B1020 Mooncake(25)
IDEA执行Test操作导致数据插入时出现了重复数据
正则表达式replaceAll()方法具有什么功能呢?
2.4G无线收发模块的应用
只有一台交换机,如何实现主从自动切换之nqa
Does flink cdc support synchronization from oracle dg library?
matcher中find,matches,lookingAt匹配字符串的不同之处说明
我的杂记链接
Which big guy has the 11G GI and ojvm patches in April or January 2020, please help?
Seata source code analysis: initialization process of TM RM client
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
How to realize the short press and long press detection of the button?
Redis源码解析:Redis Cluster
There is only one switch, how to realize the nqa of master-slave automatic switching
Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
微服务 技术栈
为什么sys_class 里显示的很多表的 RELTABLESPACE 值为 0 ?
Science bosses say | Hong Kong rhubarb KaiBin teacher take you unlock the relationship between the matrix and 6 g