定义:将不同细粒度的函数组合起来,按照一定的执行顺序依次执行,通过接收对应的参数触发函数,最终返回结果。
参考lodash组合函数
/**
* @title lodash函数组合的方法
* flow() 从左到右执行
* flowRight() 从右到左执行
*
*/
const _ = require("lodash");
const reverse = (array) => array.reverse();
const first = (array) => array[0];
const toUpper = (value) => value.toUpperCase();
// flowRight会依次从右到左执行
const flowRight = _.flowRight(toUpper, first, reverse);
console.log(flowRight(["a", "b", "casdas"]));
简单实现组合函数
const compose = (...args) => values => args.reverse().reduce((prev, current)=> current(prev), values)
const reverse = (array) => array.reverse();
const first = (array) => array[0];
const toUpper = (value) => value.toUpperCase();
const flowRight = compose(toUpper ,first ,reverse);
console.log(flowRight(["a", "b", "casdas"]));