Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
reduceRight with an array of functions in Javascript?

What does this code do with the list of functions?

store = {};

mw = [f1, f2, f3];

ds = mw.map(function (fn) {
    return fn.bind(null, store);
});

ds2 = ds.reduceRight(function (dispatch, fn) {
    return fn.bind(null, dispatch);
});

it is combining it somehow, but I don't understand the goal. What are requirements for f* functions?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The original functions are curried twice with an argument value: once with store, and the second time with a callback function dispatch, which through reduceRight is a function that refers to the next curried function in the array.

So f* should be functions that take two arguments: the object that plays a role (for anything you want), and the callback function to be called next. The exception to this rule is the last function: it doesn't get a second argument.

Here is an example for those functions:

function f1(store, dispatch) {
    store["f1"] = "I";
    return dispatch();
}
function f2(store, dispatch) {
    store["f2"] = "was";
    return dispatch();
}
function f3(store) {
    store["f3"] = "here";
    return store;
}

var store = {};
var mw = [f1, f2, f3];
var ds = mw.map(function (fn) {
    return fn.bind(null, store);
});
var ds2 = ds.reduceRight(function (dispatch, fn) {
    return fn.bind(null, dispatch);
});
// ds2 is a function, that chains all given functions together 
//   through the callback system that they implement:
console.log(ds2());

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!