当前位置:网站首页>函数高阶-柯里化实现
函数高阶-柯里化实现
2022-07-02 18:25:00 【V_AYA_V】
在数学和计算机科学中,柯里化是一种将使用多个参数的函数转换成一系列使用一个参数的函数,并且返回接受余下的参数而且返回结果的新函数的技术。
例:实现函数 add(1,2,3) 输出6
add(x,y,z){
return x+y+z
}
// add(1,2,3) => 6
由以上事例看出分别使用x,y,z承接1,2,3执行加操作返回累加值,但是实际开发过程中可能需要实现更多数字的累加效果,此时需要将add函数进行改造,利用闭包特性实现将参数分片传入,达到最终累加效果。其实到了这就已经简单实现一个柯里化函数了。
add(a){
return function(a){
return function(b){
return function(c){
return a+b+c
}
}
}
}
// add(1)(2)(3) => 6
显然,上述实现的柯里化函数并不能很好的扩展,当所需参数更多的时候仍然需要手动修改。注意到函数的核心是实现累加效果,并不在意传入参数多少,因此可以考虑使用递归方法再次扩展上述函数
let arr = []
function add() {
let arg = Array.prototype.slice.call(arguments); // 递归获取后续参数
arr = arr.concat(arg);
if (arg.length === 0) {
// 如果参数为空,则判断递归结束
return arr.reduce((a,b)=>{
return a+b}) // 求和
} else {
return add;
}
}
add(1)(2)(3)()
封装一个将普通函数转化为柯里化函数的方法
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)
边栏推荐
- Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others
- PyTorch函数中的__call__和forward函数
- PHP parser badminton reservation applet development requires online system
- Fastdfs installation
- GMapping代码解析[通俗易懂]
- Yunna | why use the fixed asset management system and how to enable it
- How performance testing creates business value
- 横向越权与纵向越权[通俗易懂]
- [论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
- In pytorch function__ call__ And forward functions
猜你喜欢

Processing strategy of message queue message loss and repeated message sending

PHP-Parser羽毛球预约小程序开发require线上系统

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

juypter notebook 修改默认打开文件夹以及默认浏览器

IEDA refactor的用法

Hospital online inquiry source code hospital video inquiry source code hospital applet source code

《重构:改善既有代码的设计》读书笔记(下)

数据降维——主成分分析

Use cheat engine to modify money, life and stars in Kingdom rush

《架构整洁之道》读书笔记(下)
随机推荐
A4988驱动步进电机「建议收藏」
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
虚拟机初始化脚本, 虚拟机相互免秘钥
Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
MySQL表历史数据清理总结
冒泡排序数组
Quanzhi A33 uses mainline u-boot
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
End to end object detection with transformers (Detr) paper reading and understanding
Talk about the design of red envelope activities in e-commerce system
【pytorch学习笔记】Tensor
juypter notebook 修改默认打开文件夹以及默认浏览器
IEDA refactor的用法
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Codeworks 5 questions per day (1700 average) - day 4
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
程序猿入门攻略(十二)——数据的存储
450-深信服面经1
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
End-to-End Object Detection with Transformers(DETR)论文阅读与理解