4 ms·
The assumption in this article is that as long as a line of code as the same characters, its output will be the same. But this makes no sense. Consider: va
by svalorzen 10y ago
The assumption in this article is that as long as a line of code as the same characters, its output will be the same. But this makes no sense. Consider:
var arr = [0,1,2]
sum(arr);
arr = [1,2,3];
sum(arr);
The two sums will have different results. What the article does is in essence calling
sum([Math.Random(), Math.Random(), Math.Random()]);
sum([Math.Random(), Math.Random(), Math.Random()]);
I don't see why one would expect the output of such a call to be always the same, if you are in all effect changing the input. Even if the characters you typed are the same, if the values that are passed around change the result of a function call will change. "Pure" is a property with regards to the values passed to a function, not to the characters typed to call it.
- philplckthun 10y agoI believe as well that the article is imprecise, since it elaborates on a trick question and tries to just inform about the term "pure" being hard to define, especially in a dynamic language. To be referencing an exact quote of the article to make it very clear: var arr = [{}, {}, {}]; arr[0].valueOf = arr[1].valueOf = arr[2].valueOf = Math.random; Obviously this relies on multiple function calls, so the output of the original function `sum` is still predictable. I think predictability and the absence of side effects is still a much better definition for pure functions. It's also a necessary definition in JS. Saying that 'the same call of the function with similar enough arguments will always yield the same output' is not a sufficient definition and way too simplified.