js函数组合
2024-10-12 09:10:24
- 纯函数和柯里化容易引起洋葱代码
- 函数组合可以让我们把细粒度的函数重新组合生成一个新的函数
- 函数组合并没有减少洋葱代码,只是封装了洋葱代码
- 函数组合执行顺序从右到左
- 满足结合律既可以把g和h组合 还可以把f和g组合,结果都是一样的
const _ = require("lodash");
const reverse = arr => arr.reverse()
const first = arr => arr[0]
const toUpper = s => s.toUpperCase()
const lastToupper = _.flowRight(toUpper, first, reverse)
console.log(lastToupper(['one', 'two', 'three']))
// 模拟 lodash 中的 flowRight
function compose (...args) {
return function (value) {
return args.reverse().reduce(function (acc, fn) {
console.log(fn)
return fn(acc)
}, value)
}
}
const composeEs6 = (...args) => value => args.reverse().reduce((acc, fn) => fn(acc), value)
const f = composeEs6(toUpper, first, reverse)
console.log(f(['one', 'two', 'three']))
//结合律
最新文章
- PhoneJS - HTML5 JavaScript 移动开发框架
- 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(五)Image View视图 学习笔记
- WebMethod属性详解
- R之批处理
- android boot.img 结构
- poj 2553 The Bottom of a Graph
- mysql中php生成唯一ID
- codeforces 388C 	 Fox and Card Game
- 工作总结:将电脑中的ARP缓存清空黑屏命令
- Topcoder 好题推荐
- 第二十次codeforces竞技结束 #276 Div 2
- The POM for * is invalid
- [Luogu 3414]SAC#1 - 组合数
- PS 滤镜——素描算法(二)
- Dynamics 365中的应用程序介绍
- 黑盒测试实践——day03
- C# 枚举用法
- 分布式-JOB(XXL-Job)
- Ubuntu12.04软件安装指南
- Mybatis通过GNDL语法引用静态常量或者枚举类型
热门文章
- Taurus.MVC 微服务框架 入门开发教程:项目部署:4、微服务应用程序发布到Docker部署(上)。
- flutter系列之:构建Widget的上下文环境BuildContext详解
- 理解C++函数指针和指针函数(一)
- Linux KVM创建虚拟机
- InnoDB_锁总结
- LSB隐写术
- sklearn中MLPClassifier源码解析
- 放弃 Electron,拥抱 WebView2!JavaScript 快速开发独立 EXE 程序
- 我的 Kafka 旅程 - Linux下的安装 &; 基础命令
- Promtail 配置文件说明