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.
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.
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.
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.
A function must pass two tests to be considered “pure”: Same inputs always return same outputs. No side-effects.
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.
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 ...
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.
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.
I think it depends on getVat()
. If getVat()
is pure addVat()
is pure too :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With