CodeNot attempted
pipe and compose
Difficulty: easy
pipe(f, g, h)(x) is h(g(f(x))); compose is the same right-to-left. The first function may take several arguments; with no functions, return the input unchanged.
Show reference solution
export function pipe(...fns: ((...args: any[]) => any)[]): (...args: any[]) => any { return function (this: unknown, ...args: any[]) { if (fns.length === 0) { return args[0]; } // The first function gets all the arguments, every next one gets the previous result. let result = fns[0].apply(this, args); for (let i = 1; i < fns.length; i++) { result = fns[i].call(this, result); } return result; };}
// compose is pipe from right to left.export function compose(...fns: ((...args: any[]) => any)[]): (...args: any[]) => any { return pipe(...fns.reverse());}
Ctrl Enter