当前位置:网站首页>Function high order curry realization
Function high order curry realization
2022-07-02 19:33:00 【V_ AYA_ V】
In mathematics and Computer Science , Currierization is the transformation of a function with multiple parameters into a series of functions with one parameter , And return the technique of taking the remaining arguments and returning the result of the new function .
example : Implementation function add(1,2,3) Output 6
add(x,y,z){
return x+y+z
}
// add(1,2,3) => 6
It can be seen from the above examples that x,y,z Undertaking 1,2,3 Execute the add operation to return the accumulated value , However, in the actual development process, it may be necessary to achieve the cumulative effect of more numbers , At this time, we need to add Function to change , Using the closure feature to realize the parameter partition into , Achieve the final cumulative effect . In fact, at this point, we have simply implemented a Coriolis function .
add(a){
return function(a){
return function(b){
return function(c){
return a+b+c
}
}
}
}
// add(1)(2)(3) => 6
obviously , The curry function implemented above cannot be well extended , Manual modification is still needed when more parameters are required . Notice that the core of the function is to achieve the cumulative effect , It doesn't matter how many parameters are passed in , Therefore, you can consider using recursive method to extend the above function again
let arr = []
function add() {
let arg = Array.prototype.slice.call(arguments); // Get subsequent parameters recursively
arr = arr.concat(arg);
if (arg.length === 0) {
// If the argument is null , Then judge the end of recursion
return arr.reduce((a,b)=>{
return a+b}) // Sum up
} else {
return add;
}
}
add(1)(2)(3)()
Encapsulate a method to convert ordinary functions into Coriolis functions
add(x,y,z){
return x+y+z
}
MyCurried(fn){
return function curried1(...args1){
if(args1.length >= fn.length){
return fn.apply(this, args1)
}else{
return function curried2(...args2){
return curried1.apply(this,[...args1,...args2])
}
}
}
}
const _add = MyCurried(add)
_add(1)(2)(3)
_add(1,2)(3)
边栏推荐
- 虚拟机初始化脚本, 虚拟机相互免秘钥
- AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
- Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
- Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
- Gmapping code analysis [easy to understand]
- PHP-Parser羽毛球预约小程序开发require线上系统
- [error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
- MySQL advanced (Advanced) SQL statement
- Develop fixed asset management system, what voice is used to develop fixed asset management system
- juypter notebook 修改默认打开文件夹以及默认浏览器
猜你喜欢
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
Bubble sort array
开发固定资产管理系统,开发固定资产管理系统用什么语音
Yolov3 trains its own data set to generate train txt
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
Markdown basic grammar
Advanced performance test series "24. Execute SQL script through JDBC"
High frequency interview questions
程序猿入门攻略(十二)——数据的存储
随机推荐
PHP asymmetric encryption method private key and public key encryption and decryption method
[pytorch learning notes] tensor
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Chapter 7 - class foundation
451-memcpy、memmove、memset的实现
Juypter notebook modify the default open folder and default browser
【ERP软件】ERP体系二次开发有哪些危险?
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
metric_ Logger urination
Qpropertyanimation use and toast case list in QT
metric_logger小解
"Patient's family, please come here" reading notes
Watchful pioneer world outlook Architecture - (how does a good game come from)
MySQL table historical data cleaning summary
第七章-类基础
AcWing 1127. 香甜的黄油 题解(最短路—spfa)
Emmet basic syntax
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
虚拟机初始化脚本, 虚拟机相互免秘钥