如何实现compose函数并通过ES6进行优化

内容

代码

JavaScript版本

function compose (...args) {
    return function (value) {
        return args.reverse().reduce(function(acc, fn) {
            return fn(acc)
        }, value)
    }
}

ES6版本

const compose = (...args) => value => args.reverse().reduce((acc, fn) => fn(acc), value)

测试

const f = compose(toUpper, first, reverse)
console.log(f(['one', 'two']))

JavaScript——实现compose函数

相关文章:

  • 2022-02-28
  • 2021-06-18
  • 2021-09-17
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2022-12-23
  • 2022-03-05
相关资源
相似解决方案