• 纯函数和柯里化容易引起洋葱代码
  • 函数组合可以让我们把细粒度的函数重新组合生成一个新的函数
  • 函数组合并没有减少洋葱代码,只是封装了洋葱代码
  • 函数组合执行顺序从右到左
  • 满足结合律既可以把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']))
//结合律

原文地址: https://kspf.xyz/archives/71

最新文章

  1. PhoneJS - HTML5 JavaScript 移动开发框架
  2. 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(五)Image View视图 学习笔记
  3. WebMethod属性详解
  4. R之批处理
  5. android boot.img 结构
  6. poj 2553 The Bottom of a Graph
  7. mysql中php生成唯一ID
  8. codeforces 388C Fox and Card Game
  9. 工作总结:将电脑中的ARP缓存清空黑屏命令
  10. Topcoder 好题推荐
  11. 第二十次codeforces竞技结束 #276 Div 2
  12. The POM for * is invalid
  13. [Luogu 3414]SAC#1 - 组合数
  14. PS 滤镜——素描算法(二)
  15. Dynamics 365中的应用程序介绍
  16. 黑盒测试实践——day03
  17. C# 枚举用法
  18. 分布式-JOB(XXL-Job)
  19. Ubuntu12.04软件安装指南
  20. Mybatis通过GNDL语法引用静态常量或者枚举类型

热门文章

  1. Taurus.MVC 微服务框架 入门开发教程:项目部署:4、微服务应用程序发布到Docker部署(上)。
  2. flutter系列之:构建Widget的上下文环境BuildContext详解
  3. 理解C++函数指针和指针函数(一)
  4. Linux KVM创建虚拟机
  5. InnoDB_锁总结
  6. LSB隐写术
  7. sklearn中MLPClassifier源码解析
  8. 放弃 Electron,拥抱 WebView2!JavaScript 快速开发独立 EXE 程序
  9. 我的 Kafka 旅程 - Linux下的安装 & 基础命令
  10. Promtail 配置文件说明