Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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.

like image 290
Jose Marin Avatar asked Oct 24 '21 17:10

Jose Marin


People also ask

How do you know if a function is pure or not?

A pure function is a function where the return value is only determined by its input values, without observable side effects. When a function performs any other “action”, apart from calculating its return value, the function is impure. Single impure function would contaminate any function that calls it.

Which is not a pure function?

Any function that affects any state other than that of local variables is a non-pure function. Here foo only affects state by taking the input arguments and producing an output value. The original some_list object was not mutated either, a new object was returned instead.

What is a pure function?

In computer programming, a pure function is a function that has the following properties: the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams), and.

Which of the following rules are correct for pure functions?

A function must pass two tests to be considered “pure”: Same inputs always return same outputs. No side-effects.

What is a pure function?

the function application has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams). Thus a pure function is a computational analogue of a mathematical function.

How to pass a function as a parameter in JavaScript?

Passing a function as a parameter in JavaScript. Passing a function as an argument to the function is quite similar to the passing variable as an argument to the function. Below examples describes to passing a function as a parameter to another function. Example 1: This example passes a function geeks_inner to the function geeks_outer as an ...

How to pass a function as an argument to another function?

Passing a function as an argument to the function is quite similar to the passing variable as an argument to the function. so variables can be returned from a function. The below examples describe passing a function as a parameter to another function. Example 1: This example passes a function geeks_inner to the function geeks_outer as an argument.

How to call a function as a parameter of another function?

Here's how to call a function (or procedure) as a parameter of another function (or procedure) : Declare the function (or procedure) that will be used as a parameter. In the example below, this is "TFunctionParameter". Define a function that will accept another function as a parameter.


1 Answers

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

like image 90
Patryk Avatar answered Oct 21 '22 16:10

Patryk