News for hackers. Stuff that matters.
Re:Is your JavaScript function actually pure? by agd on Tuesday September 20, 2016 @06:49AM (#12538480)
I think it's commonly agreed that pure functions depend only on their inputs AND cannot have side effects. So when introducing 'Math.random' the function would be impure as it has side effects.
Re:Is your JavaScript function actually pure? by grenoire on Tuesday September 20, 2016 @07:19AM (#12538612)
Is it then the variable (or the variable accessor) that is impure, or the function that is impure?
Re:Is your JavaScript function actually pure? by 14113 on Tuesday September 20, 2016 @10:55AM (#12539971)
The variable accessor is a function of type Variable -> Field, which the function sum is calling. Because of that, the function sum is impure, as it's calling an impure function. I would argue that the object is just a wrapper around a function, and in essence making the sum function implicitly higher order, as you're passing it a function to retrieve a value, rather than an actual value.
Re:Is your JavaScript function actually pure? by nbevans on Tuesday September 20, 2016 @09:20AM (#12539278)
Math.random doesn't necessarily have side affects. If it's storing a seed value, and incrementing that each time; then yes it stores mutable state which is a side affect because future invocations of that function will then depend upon that pre-existing state.

If however Math.random is passively listening for background radiation through some sensor perhaps then it doesn't really have side affects does it? Nor does it have mutable state, for that matter.

But it would still be impure according the true definition of what is a pure function, I believe.

Re:Is your JavaScript function actually pure? by kimolas on Tuesday September 20, 2016 @10:21AM (#12539706)
I thought it was a bit pedantic as well, at first. But looking at Haskell, I can see why the author made this point. Haskell functions won't accept random values as input, since they're monadic values.
Re:Is your JavaScript function actually pure? by DasIch on Tuesday September 20, 2016 @12:16PM (#12540671)
Are there two identical inputs to Math.random that yield different results? If the answer to that question is yes, the function isn't pure.
Re:Is your JavaScript function actually pure? by pwdisswordfish on Tuesday September 20, 2016 @10:55AM (#12539978)
Replace Math.random with this, then:

    ((j) => () => j++)(0)