当前位置:网站首页>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)
边栏推荐
猜你喜欢
End to end object detection with transformers (Detr) paper reading and understanding
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
IEDA refactor的用法
Codeworks 5 questions per day (1700 average) - day 4
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
高级性能测试系列《24. 通过jdbc执行sql脚本》
数据降维——主成分分析
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
随机推荐
zabbix5客户端安装和配置
Golang:[]byte to string
453-atoi函数的实现
AcWing 1129. 热浪 题解(最短路—spfa)
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
C文件输入操作
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
AcWing 1135. 新年好 题解(最短路+搜索)
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
SIFT feature point extraction "suggestions collection"
Processing strategy of message queue message loss and repeated message sending
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
安装单机redis详细教程
450-深信服面经1
MySQL表历史数据清理总结
搭建哨兵模式reids、redis从节点脱离哨兵集群
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
AcWing 1134. 最短路计数 题解(最短路)
Emmet基础语法
Digital scroll strip animation