const compose = (...fns) =>
fns.reduceRight((prevFn, nextFn) =>
(...args) => nextFn(prevFn(...args)),
value => value
);Create the function, composed of three others:
const example = compose(
val => { console.log(1); return `1<${val}>`; },
val => { console.log(2); return `2<${val}>`; },
val => { console.log(3); return `3<${val}>`; }
);Call the function:
example('hello')Console output is:
3
2
1
"1<2<3<hello>>>"
If only there was a way to type this function over an arbitrary length tuple in TS.
Also, would it make sense, for performance, to define static returns for a finite set of lengths?
To make
compose(h,g,f)return(...args)=>h(g(f(...args)))instead of(...args)=>h(((...args)=>g(((...args)=>f(...args))(...args)))(...args))