Convert an array of functions into nested function calls

Make a new function, which chains all the functions in the array:

funcx = funcs.reduceRight((f2, f1) => x => f1(f2(x)));

Then call this function:

funcx(); 

Of course, you could do it also in on step, but it might be a bit confusing:

funcs.reduceRight((f2, f1) => x => f1(f2(x)))();

If you want to be prepared for an empty array of functions, you can do it like this:

funcs.reduceRight((f2, f1) => x => f1(f2(x)), x => 0)();

Tags:

Javascript