当前位置:网站首页>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)
边栏推荐
- 2022 compilation principle final examination recall Edition
- Develop fixed asset management system, what voice is used to develop fixed asset management system
- Data dimensionality reduction factor analysis
- Chapter 7 - class foundation
- Microservice technology - distributed global ID in high concurrency
- 《代碼整潔之道》讀書筆記
- NPOI导出Excel2007
- LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
- 潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
- 2022.7.1-----leetcode.241
猜你喜欢

Yolov3 trains its own data set to generate train txt

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

High frequency interview questions

Usage of ieda refactor

Yunna | why use the fixed asset management system and how to enable it

Quanzhi A33 uses mainline u-boot

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management

机器学习笔记 - 时间序列预测研究:法国香槟的月销量

AcWing 1126. 最小花费 题解(最短路—dijkstra)

Py之interpret:interpret的简介、安装、案例应用之详细攻略
随机推荐
AcWing 1135. 新年好 题解(最短路+搜索)
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
Advanced performance test series "24. Execute SQL script through JDBC"
mysql函数
In pytorch function__ call__ And forward functions
Refactoring: improving the design of existing code (Part 1)
C文件输入操作
虚拟机初始化脚本, 虚拟机相互免秘钥
2022.7.1-----leetcode.241
2022 compilation principle final examination recall Edition
Use cheat engine to modify money, life and stars in Kingdom rush
Emmet basic syntax
AcWing 341. 最优贸易 题解 (最短路、dp)
二进制操作
Watchful pioneer world outlook Architecture - (how does a good game come from)
Introduction of Ethernet PHY layer chip lan8720a
以太网PHY层芯片LAN8720A简介
搭建哨兵模式reids、redis从节点脱离哨兵集群
AcWing 1126. 最小花费 题解(最短路—dijkstra)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径