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

113
Visualizações
Currying and reduce in javascript

Why at first I get 80 then 88 and then 90? Can anyone explain in detail what is happening in the compose function?

const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));


const sum = a => b => a + b
const multiply = a => b => a * b

const addTransactionFee = sum(2)
const addTax = multiply(1.1)
const addMonthlyPromotion = multiply(0.8)
const log = a => {
  console.log(a)
  return a
}

const paymentAmount = compose(
  log,
  addTransactionFee, 
  log,
  addTax,
  log,
  addMonthlyPromotion
)(100)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use recursion instead. This way the order of functions is preserved (and not reversed).

const recursion = (value, func, ...fns) =>
  func ? recursion(func(value), ...fns) : value;

const paymentAmount = (amount) =>
  recursion(
    amount,
    log,
    addTransactionFee,
    log,
    addTax,
    log,
    addMonthlyPromotion,
    log
  );

about 4 years ago · Juan Pablo Isaza Relatório

0

The compose function with the loop of reduce..take the right function (the latest (addMonthlyPromotion())) and make the result of 100 * 0.8 so you have 80 at the first loop. Then you have the second loop with the second function (addTax()) it multiply's the previous result of (80) with 1.1 so 88. And the final loop and the 3rd one of reduce with the function (addTransactionFee()) add 2 in your previous number so 90.

Now the compose function goes from right to left of looped function and do the calculations.

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