函数组合 要满足一个特点: 结合律 associativity

这里的结合律其实就是数学概念上的结合律, 如下

  1. const f = compose(a, b, c);
  2. let associativity = compose(a , compose(b,c)) == compose(compose(a,b),c);
  3. // associativity -> true

即 我们可以先把 b、c 组合 , 也可以先把 a 、b 组合,执行的结果是一样的。

  1. const _ = require('loadsh');
  2. const lastaUpperFn = _.flowRight(_.toUpper , _.first, _.reverse);
  3. const lastUpperFn1 = _.flowRight(_.flowRight(_.toUpper , _.first) , _.reverse);
  4. const lastUpperFn2 = _.flowRight(_.toUper,_.flowRight(_.first,_.reverse));
  5. const arr = ['one' , 'two' , 'three'];
  6. console.log(lastaUpperFn(arr));
  7. console.log(lastaUpperFn1(arr));
  8. console.log(lastaUpperFn2(arr));