定义:将不同细粒度的函数组合起来,按照一定的执行顺序依次执行,通过接收对应的参数触发函数,最终返回结果。
    参考lodash组合函数

    1. /**
    2. * @title lodash函数组合的方法
    3. * flow() 从左到右执行
    4. * flowRight() 从右到左执行
    5. *
    6. */
    7. const _ = require("lodash");
    8. const reverse = (array) => array.reverse();
    9. const first = (array) => array[0];
    10. const toUpper = (value) => value.toUpperCase();
    11. // flowRight会依次从右到左执行
    12. const flowRight = _.flowRight(toUpper, first, reverse);
    13. console.log(flowRight(["a", "b", "casdas"]));

    简单实现组合函数

    1. const compose = (...args) => values => args.reverse().reduce((prev, current)=> current(prev), values)
    2. const reverse = (array) => array.reverse();
    3. const first = (array) => array[0];
    4. const toUpper = (value) => value.toUpperCase();
    5. const flowRight = compose(toUpper ,first ,reverse);
    6. console.log(flowRight(["a", "b", "casdas"]));