当前位置:网站首页>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
边栏推荐
- There is only one switch, how to realize the nqa of master-slave automatic switching
- 茄子科技CEO仇俊:以用户为中心,做用户真正需要的产品
- 周报2022-8-4
- Excuse me, guys, is it impossible to synchronize two databases in real time using Flink SQL CDC?
- 无题六
- 这样写有问题吗?怎么在sql-client 是可以做到数据的同步的
- CCVR基于分类器校准缓解异构联邦学习
- PAT乙级-B1019 数字黑洞(20)
- 无题十三
- 深度学习21天——卷积神经网络(CNN):服装图像分类(第3天)
猜你喜欢

CPU的亲缘性affinity

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

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

茄子科技CEO仇俊:以用户为中心,做用户真正需要的产品

Creo 9.0 基准特征:基准轴

dotnet OpenXML parsing PPT charts Getting started with area charts

Seata source code analysis: initialization process of TM RM client

Weekly Report 2022-8-4

dotnet OpenXML 解析 PPT 图表 面积图入门

5.部署web项目到云服务器
随机推荐
基于 Kubernetes 的微服务项目整体设计与实现
无题八
Oracle临时表空间作用
PAT乙级-B1019 数字黑洞(20)
The Seven Weapons of Programmers
Seata source code analysis: initialization process of TM RM client
我的杂记链接
Going to book tickets tomorrow, ready to go home~~
正则表达式replaceAll()方法具有什么功能呢?
5. Deploy the web project to the cloud server
Example of Noise Calculation for Amplifier OPA855
百行代码发射红心,程序员何愁命不中女朋友!
科普大佬说 | 港大黄凯斌老师带你解锁黑客帝国与6G的关系
【零基础玩转BLDC系列】无刷直流电机无位置传感器三段式启动法详细介绍及代码分享
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
无题十二
Advanced usage of C language
无题十三
无题七
【ASM】字节码操作 方法的初始化 Frame