const waterfall = (...functions) => (callback, ...args) =>
functions.reduceRight(
(composition, fn) => (...results) => fn(composition, ...results),
callback
)(...args);
const randInt = max => Math.floor(Math.random() * max)
const add5 = (callback, x) => {
setTimeout(callback, randInt(1000), x + 5);
};
const mult3 = (callback, x) => {
setTimeout(callback, randInt(1000), x * 3);
};
const sub2 = (callback, x) => {
setTimeout(callback, randInt(1000), x - 2);
};
const split = (callback, x) => {
setTimeout(callback, randInt(1000), x, x);
};
const add = (callback, x, y) => {
setTimeout(callback, randInt(1000), x + y);
};
const div4 = (callback, x) => {
setTimeout(callback, randInt(1000), x / 4);
};
const computation = waterfall(add5, mult3, sub2, split, add, div4);
computation(console.log, 5) // -> 14
// same as:
const computation2 = (input, callback) => {
const f6 = x=> div4(callback, x);
const f5 = (x, y) => add(f6, x, y);
const f4 = x => split(f5, x);
const f3 = x => sub2(f4, x);
const f2 = x => mult3(f3, x);
add5(f2, input);
}
I understand the workings around .reduceRight()
, however the sample code on running a list of asynchronous function is quite confusing to me as from the looks of it it seems like it is not following the syntax provided by the doc but somehow it worked. Where did the composition
comes from? What about the ...args
, is it right to assume the ...args
is 5
from computation(console.log, 5)
? Also I'm pretty confused by the purpose of why is there an invoked paranthesis (...args)
on const waterfall
?
Also one side question, is it necessary to read the entire MDN doc to fully understand JavaScript? I'm planning to read the entire doc but somehow I have been reading for almost weeks and I see no end to it.