当前位置:网站首页>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)
边栏推荐
- 移动机器人路径规划:人工势场法[通俗易懂]
- Emmet basic syntax
- Fastdfs installation
- 股票证券公司排名,有安全保障吗
- 4274. 后缀表达式-二叉表达式树
- Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
- Reading notes of code neatness
- AcWing 1126. 最小花费 题解(最短路—dijkstra)
- 横向越权与纵向越权[通俗易懂]
- xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
猜你喜欢

《架构整洁之道》读书笔记(下)

高级性能测试系列《24. 通过jdbc执行sql脚本》

Data dimensionality reduction principal component analysis

zabbix5客户端安装和配置
冒泡排序数组

Watchful pioneer world outlook Architecture - (how does a good game come from)

搭建主从模式集群redis

为什么要做企业固定资产管理系统,企业如何加强固定资产管理

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统

Excel finds the same value in a column, deletes the row or replaces it with a blank value
随机推荐
What is 9D movie like? (+ common sense of dimension space)
Golang concurrent programming goroutine, channel, sync
zabbix5客户端安装和配置
PHP-Parser羽毛球预约小程序开发require线上系统
metric_logger小解
二进制操作
Excel finds the same value in a column, deletes the row or replaces it with a blank value
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
Gamefi链游系统开发(NFT链游开发功能)丨NFT链游系统开发(Gamefi链游开发源码)
MySQL
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
Juypter notebook modify the default open folder and default browser
AcWing 341. 最优贸易 题解 (最短路、dp)
Refactoring: improving the design of existing code (Part 1)
第七章-类基础
AcWing 383. 观光 题解(最短路)
Quanzhi A33 uses mainline u-boot
MySQL table historical data cleaning summary
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统