Hacker News new | ask | show | jobs
by tel 3564 days ago
It'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.
1 comments

It'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".
I 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.
> 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.

The 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.
> 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!

Right, 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.