I think you are looking for compose
. It looks something like this:
const compose = function (...fns) { const rest = fns.reverse(); const first = rest.shift(); return function (...args) { return rest.reduce((acc, f)=>f.call(this, acc), first.apply(this, args)); }; };
Now you can create such functions:
const stringDouble = compose(String, x=>x*2); stringDouble("44"); //==> "88" ["22","33","44"].map(stringDouble); //=> ["44", "66", "88"]
And in your case, you can write your function as follows:
const mapMany = function(processors, channels){
The advantage over your own code and other answer with reduce
is that it does not make processors.length
arrays in this process, but only that one.
There are libraries that supply compose
. This is a common function in functional programming.
Other display functions, such as those found in Underscore, allow you to set this
. Then the class methods will work when I pass this
to the main functions.
source share