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

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

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 Denunciar

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 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