Is your JavaScript function actually pure?
Reposted from <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12530652" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12530652</a><p>I disagree with some of the claims, as I think these sorts of edge-cases mostly serve to highlight how imprecise the given pure&#x2F;impure distinction is, and how we can&#x27;t always take a semantic concept from one language (e.g. Haskell) and apply it in another (JS).<p>For example, if you always get &quot;TypeError&quot; when calling &quot;sum()&quot;, you can replace &quot;sum()&quot; with its result (&quot;throw TypeError&quot;) and get the same behaviour; it doesn&#x27;t break referential transparency, so is it &quot;pure&quot;? What do we even mean by &quot;result&quot; in a language with exception handling? Do thrown exceptions count, or only return values?<p>Similarly, using &quot;valueOf&quot; doesn&#x27;t show that the sum function is impure, it shows that the sum function is higher-order. If we mapped &#x27;Math.random&#x27; over an array, does that make &#x27;map&#x27; impure? What does it mean to be pure&#x2F;impure when any variable can be replaced by a &#x27;valueOf&#x27; function&#x2F;procedure?<p>If two expressions call the same procedures, the same number of times, in the same order, and return the same pure function of their outputs, are those two expressions the same? Would replacing one with the other maintain referential transparency, and does that imply &quot;purity&quot;?<p>I think the answer is that such examples need more fine-grained notions than just &quot;pure&quot;&#x2F;&quot;impure&quot;. Otherwise it&#x27;s just arguing over the semantics of English, rather than semantics of programs.