zoukankan      html  css  js  c++  java
  • js函数组合

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

  • 相关阅读:
    十天冲刺4
    单词统计
    十天冲刺3
    学习进度第十周
    十天冲刺2
    十天冲刺1
    梦断代码阅读笔记03
    学习进度第九周
    [强网杯 2019]Upload
    [2020 新春红包题]1
  • 原文地址:https://www.cnblogs.com/fengbaba/p/15106230.html
Copyright © 2011-2022 走看看