CódigoSin intentar
pipe y compose
Dificultad: fácil
pipe(f, g, h)(x) es h(g(f(x))); compose es lo mismo de derecha a izquierda. La primera función puede recibir varios argumentos; sin funciones, devuelve la entrada sin cambios.
Ver solución de referencia
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