Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda