当前位置:网站首页>Why do I say: curry = = closure + recursion?
Why do I say: curry = = closure + recursion?
2022-08-07 08:54:00 【InfoQ】
function sum(a,b){
console.log(a+b)
}
sum(1,2)
function sum(a,b,c){
console.log(a+b+c)
}
sum(1,2,3)
- What is the "opening and closing principle"?That is: we should avoid directly modifying functions, classes or modules in our programming, but should expand it on the original basis;
- whatIs it the "Single Responsibility Principle"?That is: each function, class or module should only be responsible for a single function;
// Responsible for adding two numbers
function sum2(a,b){
console.log(a+b)
}
// Responsible for adding three numbers
function sum3(a,b,c){
console.log(a+b+c)
}
// is responsible for "addition",
function addCurry(){
...
...
...
}
addCurry(1)(2) // Add two numbers
addCurry(1)(2)(3) // Add three numbers
...
addCurry(1)(2)(3)...(n) //Adding n numbers
function directHandle(a,b){
console.log("direct processing",a,b)
}
directHandle(111,222)
//Direct handle 111 222
function delayHandle(a){
return function(b){
console.log("DelayHandle",a,b)
}
}
delayHandle(111)
// ƒ (b){
// console.log("delayed processing",a,b)
// }
delayHandle(111)directHandle(111,222)f(b)delayHandle(111)(222)//Add two numbers
function addCurry(a){
return function(b){
console.log(a+b)
}
}
addCurry(1)(2)
// Add three numbers
function addCurry(a){
return function(b){
return function(c){
console.log(a+b+c)
}
}
}
addCurry(1)(2)(3)
return function
let arr = []
function addCurry() {
let arg = Array.prototype.slice.call(arguments); // Get subsequent parameters recursively
arr = arr.concat(arg);
if (arg.length === 0) { // If the parameter is empty,Then judge the end of the recursion
return arr.reduce((a,b)=>{return a+b}) //summation
} else {
return addCurry;
}
}
addCurry(1)(2)(3)()
compose(fn1)(fn2)(fn3)…(fnN)(args)边栏推荐
- Knapsack Theory 01 Knapsack (Rolling Array)
- Today's sleep quality record 74 points
- The principle and source code of redis-redis memory elimination strategy & LRU source code analysis & LFU source code analysis
- 一种API写法
- 3D~RPG游戏的制作
- redis的原理和源码-集群的原理和源码解析(上)
- 10 things he learned after teaching most of his life at MIT
- ABP 6.0.0-rc.1的新特性
- window.requestAnimationFrame Web3D渲染帧率控制
- 帕累托分析中的累计优化
猜你喜欢

Model fine-tuning transfer learning Finetune method Daquan

2022 TV cup mathematical modeling - online documentation

3D~RPG游戏的制作

ABP 6.0.0-rc.1的新特性

Transport layer (UDP protocol, TCP protocol three-way handshake, four-way wave)

Vitalik explains 5 types of ZK-EVM

The principle and source code of redis-sentinel sentinel principle and source code analysis (on)

STM32出现波特率不对问题,我们要设定的波特率是9600,而实际的波特率竟然是14400,这是为什么呢?

双向链表的增删查改

U-Net网络
随机推荐
你如何看待抖音的中视频伙伴计划的?
3D~RPG game production
背包理论之01背包
JVM:(五)运行时数据区之虚拟机栈
STM32出现波特率不对问题,我们要设定的波特率是9600,而实际的波特率竟然是14400,这是为什么呢?
The second bullet of FPGA development: running water lamp experiment
FPGA development fourth bullet: touch button to control LED light experiment
LeetCode #100. 相同的树
关于在物联网公司实习这几天的收获
运筹学基础【二】 之 预测
曾“伪造”Solana七成TVL的“多重人格者”,正望向Aptos
【LeetCode】Day110- Divide the letter interval
3D~RPG游戏的制作
RestTemplate
Scala——While和do..While循环控制
在 MIT 教了大半辈子书,他学会 10 件事
redis的原理和源码-sentinel哨兵的原理和源码解析(上)
2022-08-07周总结
MySQL:索引的底层实现 | 聚集索引和非聚集索引 | 自适应哈希索引
Arthas use error