If however Math.random is passively listening for background radiation through some sensor perhaps then it doesn't really have side affects does it? Nor does it have mutable state, for that matter.
But it would still be impure according the true definition of what is a pure function, I believe.
((j) => () => j++)(0) f => f()
is a pure function? If so, then so is the author's example. If not, then I don't see how any JS function that takes an argument (and evaluates it) can be pure. Under that definition, I'm not sure it makes sense to talk about.A JS function could take an argument, use it but not evaluate it (or not evaluate it in all cases). `typeof` doesn't evaluate its argument for instance.
For theoretical purposes it might make sense to say pure functions cannot call impure functions, but I think a more practical definition includes functions that are provided as arguments.
I'd thought pureness was a property of function definitions, irrespective of what they get passed, but living in Javascript land I've never had occasion to be concerned with the distinction. I kind of suspect it's not really something that's useful to talk about.
Purity is a spectrum, not a binary, The more impurities you add, the more likely your program will not behave reasonably (as in, not in accordance with the simple logical rules of referential transparency).
Which part of it isn't? The distinction between "potentially non-terminating" and its negation is exactly the distinction between partial and total functions.
To be sure, but there's a big difference between casual definitions being subtly wrong and:
> I mean that it's not even so well defined mathematically.
Most mathematical definitions, at least the interesting ones, aren't really suitable for casual conversation!
The best casual definition I know of is "a pure function, f, is one such that all variation in the function's result arises from variation in its input and that all information from the function's call is contained in its result: throwing the result away is equivalent to having never called the function"
A bit of a mouthful, but it's intuitive and arises from a formal definition that works out pretty nicely. Otoh, it's complex enough that it's rarely used in its complete form. Finally, it's still subtle enough that it can lead to questions about what side effects are exactly (non-termination is an immediate example).
Anyway, I just want to continue to suggest that understanding pure functions, really, is a more difficult task than people make it out to be.
It says "same argument value(s)", but how does that apply when the argument is a function? The article seems to be saying that if the same argument is passed in every time, then the function is only pure if it returns the same value every time, regardless of whether the argument is a pure function or not.
> This article needs additional citations for verification ... (July 2014)