3 ms·
Throwing an error doesn't make a function impure. If the calculation is erroneous then the expression sum() has no value which could replace it. I.e. the defin
by kazinator 10y ago
Throwing an error doesn't make a function impure. If the calculation is erroneous then the expression sum() has no value which could replace it. I.e. the definition of pure being that we can replace sum(a) by its value without changing the behavior of the program, simply doesn't apply. The exception is effectively equivalent to the situation of there not being a value.
As for this:
var a = {}; a.valueOf = Math.random;
var fn = x => x * 10;
fn(a); // 5.107926817373938
fn(a); // 3.4100775757245416
fn(a); // 5.1903831613695095
// Same input, different outputs!
Calling that "same input" just because it has the same syntax "a" is just ... plain silly. It's obviously "a" that is impure not the "fn" multiplication function. "a" is behaving like a macro which denotes the retrieval of a pseudo-random number.
It's simply not reasonable extend the definition of a functions' purity to include *the behavior of the expression(s) which are evaluated to provide its arguments.
(I would say that it's still not reasonable even under non-strict evaluation.)