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

109
Views
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 answers
Answer question

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 Report

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