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

191
Views
Does a function that receives a function as a parameter can still be considered a pure function?

is addVat a pure function?

function addVat(country, getVat, amount)
{
   if(amount > 0) {
     return amount + getVat(country)*amount
   }
   
   return amount
}

Having a function as a parameter - getVat - makes addVat automatically impure?

Depending on the implementation of getVat, we could introduce any side effects... getVat could return a random value making the result of addVat unpredictable.

const getVat = ()=> Math.random()
addVat('UK',getVat, 20)

Case of producing external side-effects...

const getVat = ()=> {
  updateDbWithVatUsed(0.2)
  return 0.2
}
addVat('UK',getVat, 20)

By other hand, the unit testing is feasible as side effects or data variability is outside of the pure function and we can stub this to make testing totally predictable.

const getUKVat = ()=> 0.2
assert.equal(addVat('UK',getUkVat, 20), 24)

My doubts come from... is purity defined as what the function does excluding anything what happens to an external call that has been passed as a parameter?

Otherwise, as high order function is a theme for Functional programming... does it not make it impossible to classify a function as pure just by itself without considering invocation considerations.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think it depends on getVat(). If getVat() is pure addVat() is pure too :)

about 4 years ago · Juan Pablo Isaza Report

0

Javascript language does not enforce purity, and hence it's impossible to guarantee purity of a function which invokes functions passed as arguments. This thread might help.

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!