纯函数和柯里化很容易造成洋葱代码函数组合允许我们重新组合细粒度函数生成新函数函数组合不会减少洋葱代码,只是封装洋葱代码函数组合执行的序列来自从右到左满足结合律。您可以组合g和h或f和g。结果是一样的。const_=require("lodash");constreverse=arr=>arr.reverse()constfirst=arr=>arr[0]consttoUpper=s=>s.toUpperCase()constlastToupper=_.flowRight(toUpper,first,reverse)console.log(lastToupper(['one','two','three']))//在lodash中模拟flowRightfunctioncompose(...args){returnfunction(value){returnargs.reverse().reduce(function(acc,fn){console.log(fn)returnfn(acc)},value)}}constcomposeEs6=(...args)=>value=>args.reverse().reduce((acc,fn)=>fn(acc),value)constf=composeEs6(toUpper,first,reverse)console.log(f(['one','two','three']))//关联法原文地址:https://kspf.xyz/archives/71
