5 ms·
> I have been adding more examples to the list asking if each example is pure or not. Seems even humans sometimes have trouble drawing a line. For example, ever
by theseoafs 11y ago
> I have been adding more examples to the list asking if each example is pure or not. Seems even humans sometimes have trouble drawing a line. For example, everyone agrees the following example is pure
> function sum(a, b) { return a + b }
> and this example is NOT
> const k = 2
> function addK(a) { return a + k }
This is wrong. Assuming the variable `k` is properly constant (never changes), `addK` is still a pure function.
The author seems to have gotten confused because they are working from an incorrect definition of what "purity" means.
- Arnavion 11y agoIt's not wrong in general. `const` is a property of the binding, not the value. If it were an object instead of the literal `2` then the author's point would be valid.
- theseoafs 11y ago> `const` is a property of the binding, not the value. This is language-specific for Javascript. > It's not wrong in general. It's not wrong in general, but it's also not right in general. The pattern is completely orthogonal to purity. Whether a closure is pure depends on a lot of factors, like the types of things in the closure and the functions it calls.
- Arnavion 11y ago>This is language-specific for Javascript. I said that in the context of the article obviously, which is about JS.