当前位置:网站首页>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
边栏推荐
猜你喜欢
15.1.1、md—md的基础语法,快速的写文本备忘录
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
Assembly language (8) x86 inline assembly
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
链表中的数字相加----链表专题
哪位大佬有20年4月或者1月的11G GI和ojvm补丁呀,帮忙发下?
CCVR基于分类器校准缓解异构联邦学习
sql server中 两表查询 平均数 分组
Xcode10的打包方式distribute app和启动项目报错以及Xcode 打包本地ipa包安装到手机上
dotnet OpenXML 解析 PPT 图表 面积图入门
随机推荐
MySQL advanced (twenty-seven) database index principle
After Keil upgrades to AC6, what changes?
汇编语言(8)x86内联汇编
Seata source code analysis: initialization process of TM RM client
ffmpeg drawtext add text watermark
【zeno】为zeno增加子模块/新节点的最小化的例子
周报2022-8-4
Excuse me, guys, is it impossible to synchronize two databases in real time using Flink SQL CDC?
Qiu Jun, CEO of Eggplant Technology: Focus on users and make products that users really need
There is only one switch, how to realize the nqa of master-slave automatic switching
my journal link
链表中的数字相加----链表专题
Why do I recommend using smart async?
leetcode points to Offer 10- I. Fibonacci sequence
Two-table query average grouping in sql server
MQTT X Newsletter 2022-07 | 自动更新、MQTT X CLI 支持 MQTT 5.0、新增 conn 命令…
Custom filters and interceptors implement ThreadLocal thread closure
七夕浪漫约会不加班,RPA机器人帮你搞定工作
Marketing Suggestions | You have an August marketing calendar to check! Suggest a collection!
Voice conversion相关语音数据集综合汇总