pipe (从左往右)
计算 **(x+2)*5**
的值
fn1 = x => x + 2
fn2 = x => 5*x
let result = pipe(fn1, fn2)(3) // 25
function pipe() {
// let args = Array.from(arguments)
let args = Array.prototype.slice.call(arguments)
return function(x) {
return args.reduce((result, cb) => {
return cb(result)
}, x)
}
}
compose (从右往左)
function compose(...args) {
return function(x) {
return args.reduceRight((result, cb) => {
return cb(result)
}, x)
}
}