组合函数, compose&pipe

 
 

  const afn = function(a) {
    return a*2 
    
  }
  const bfn = function(b) {
    return b+3
  }
 
 
  const pipe = function(...fns) {
    return function(val) {
      return fns.reduce((total, fn) => {
        return fn(total)
      }, val)
    }
  }
 
  const compose = function(...fns) {
    return function(val) {
      return fns.reverse().reduce((total, fn) => {
        return fn(total)
      }, val)
    }
  }
  const myFn = pipe(bfn,afn)
  const myFnCompose = pipe(bfn,afn)
  console.log(myFn(2))// 10
  console.log(myFnCompose(2))//  7
 
偶数
 
 
posted @ 2021-03-25 10:35  soon_k  阅读(28)  评论(0编辑  收藏  举报