6 ms·
The definition of a pure function is pretty well defined https://en.wikipedia.org/wiki/Pure_function https://en.wikipedia.org/wiki/Pure_function. Math.random de
by keeperofdakeys 10y ago
The definition of a pure function is pretty well defined https://en.wikipedia.org/wiki/Pure_function https://en.wikipedia.org/wiki/Pure_function. Math.random depends on hidden state, so (generally) any function using it isn't pure. This doesn't matter much in a language like Javascript, but other languages depend upon this definition (like the lazy function evaluation in Haskell).
- tel 10y agoIt's not so well-defined. In particular, "side effect" is hard to pin down. For instance, a good definition ought to consider potential non-termination a side effect. In that way, many functions are impure.
- keeperofdakeys 10y agoIt's a mathematical definition, of course it will break down a bit when you introduce it into the real world. I guess a more pragmatic definition would be "The ability for a runtime to delay, or repeat the execution of a function (with its arguments), and always get the same result".
- tel 10y agoI mean that it's not even so well defined mathematically. Starting from a weak place and then moving it to the "real world" makes things quite tricky.
- JadeNB 10y ago> I mean that it's not even so well defined mathematically. Which part of it isn't? The distinction between "potentially non-terminating" and its negation is exactly the distinction between partial and total functions.
- tel 10y agoThe definition of pure function (or, dually, side effect) is difficult to pin down. I've heard a number of definitions, but the best ones are far more technical than you'd see in casual conversation and the casual ones often admit holes.
- JadeNB 10y ago> I've heard a number of definitions, but the best ones are far more technical than you'd see in casual conversation 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!
- tel 10y agoRight, so I think where we're disagreeing is that the casual ones are subtly wrong. I think they're often pretty significantly wrong and that this post was essentially poking into that notion. When purity gets on the table lots of divergent ideas tend to be tossed out to nail it down and a lot of confusion results. 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.
- DougBTX 10y agoIs that definition of pure closer to mthq's definition or the one in the article? I'm not sure. 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.
- asdfaoeu 10y agoA pure function can't call a non pure function. So you can pass in a non pure function so long as you don't call it.
- rattray 10y agoPretty well defined? > This article needs additional citations for verification ... (July 2014)