JS手写练习随笔-20221210.1 ---- compose
compose - 串行组合多个方法的函数
# TS版
type IParamFn<T> = (v: T) => T; function compose<T>(...fns: IParamFn<T>[]) { if (fns.length === 0) { return (v: T) => v; } if (fns.length === 1) { return fns[0]; } const reducer = (prev: IParamFn<T>, curr: IParamFn<T>) => { return (v: T) => prev(curr(v)); }; return fns.reduce(reducer); } function fn1(x: number) { return x + 1; } function fn2(x: number) { return x + 2; } function fn3(x: number) { return x + 3; } function fn4(x: number) { return x + 4; } const vx = compose(fn1, fn2, fn3, fn4);