组合函数, 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
 
偶数
 
 
原文地址:https://www.cnblogs.com/soonK/p/14563595.html