当前位置:网站首页>js函数聚合的三种实现方式
js函数聚合的三种实现方式
2022-08-10 07:49:00 【Ares-Wang】
// 第一种:闭包存储,遍历执行
function compose1 (arr) {
return (param) => {
for (let fn of arr) {
param = fn(param);
}
return param;
}
}
// 第二种:递归嵌套,依次执行
function compose2 (arr) {
return arr.length <= 1 ? arr[0] : (...args) => compose2(arr.slice(1))(arr[0](...args))
}
// 第三种:累加嵌套,依次执行,跟第二种相似
const compose3 = (arr) => arr.reduce((res, cur) => (...args) => cur(res(...args)));
function a (p) {
return p + 1;
}
function b (p) {
return p + 6;
}
function c (p) {
return p * 2;
}
// 第四种:注意与第三章的区别
const compose4 = (arr) => arr.reduce((res, cur) => (...args) => res(cur(...args)));
const fn = compose4([a, b,c,d])
console.log(fn(1))
边栏推荐
- Using the color picker
- Power function Exponential function Logarithmic function
- 如何治理资源浪费?百度云原生成本优化最佳实践
- 【Rust指南】使用Cargo工具高效创建Rust项目 | 理解Rust特别的输入输出语句
- 34. Talk about why you want to split the database?What methods are there?
- 颜色选择器的使用
- Discussion on Chinese Fuzzy Retrieval in Databases
- CV+Deep Learning——网络架构Pytorch复现系列——classification(三:MobileNet,ShuffleNet)
- In the SQL SERVER database, if the data of the table is added, deleted, or modified, will the index of the table be recorded in the ldf log?
- 全连接神经网络结构图,神经网络示意图怎么画
猜你喜欢
随机推荐
Uni applet Tencent map polygon background transparency
DGIOT三千万电表集抄压测
winget包管理器
PHP笔记 28 29 30 31
3.1-3.3 读书笔记
颜色选择器的使用
金融证券 初级 招股书 要求 黑话1刷数 黑话2底稿 黑话3董监高
深入理解LTE网络的CDRX
手把手教你进行Mysql查询操作
VMware ESX Server常用命令行
【Rust指南】使用Cargo工具高效创建Rust项目 | 理解Rust特别的输入输出语句
IDLE development wordCount program (5)
数据库公共字段自动填充
iwemeta元宇宙:阿里首任COO:如何打造销售铁军
什么是长轮询
Introduction to the C language to realize bubble sort
If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
自动化测试框架Pytest(二)——前后置处理
Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen
[In-depth study of 4G/5G/6G topic-56]: L3 signaling control-5-radio bearer management