SOURCE

console 命令行工具 X clear

                    
>
console
let compose = (...args) => (val) => {
    args.forEach( e=> val = e(val))
    return val
}

let toUpperCase = (x) => x.toUpperCase();
 
let concatAnotherStr = (x) => x + '_by_nowell_';

let rever
let shout = compose(toUpperCase,exclaim ,(v)=> v+'___' ,(v)=> v+'@@');

v = shout('jsstr') 
console.log(v)

// 使用reduce实现
let compose2 = (...args) => {
    return args.reduce((a, b) => (arg) => a(b(arg)))
}

shout = compose2(toUpperCase,exclaim ,(v)=> v+'___', (v)=> v+'@@' );
v = shout('jsstr') 
console.log(v)

// 可以发现使用foreach的实现是按照给定函数的顺序执行的
// reduce版的是逆序执行的, 因为在reduce迭代中,后面的项成了前面方法里的参数
// 如果要保证顺序执行可以先reverse或者reduceRight
//

<body>
  <p>compose 聚合函数: 将多个函数串行执行,每个函数的结果将作为下个函数的参数</p>
  <p>可以使用forEach或者reduce两种方式,redux的中间件则采用了reduce的方式实现</p>
</body>