Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda