Is the following a pure function?
function test(min,max) {
return Math.random() * (max - min) + min;
}
My understanding is that a pure func
From mathematical point of view, your signature is not
test: ->
but
test: ->
where the environment
is capable of providing results of Math.random()
.
And actually generating the random value mutates the environment as a side effect, so you also return a new environment, which is not equal to the first one!
In other words, if you need any kind of input that does not come from initial arguments (the
part), then you need to be provided with execution environment (that in this example provides state for Math
). The same applies to other things mentioned by other answers, like I/O or such.
As an analogy, you can also notice this is how object-oriented programming can be represented - if we say, e.g.
SomeClass something
T result = something.foo(x, y)
then actually we are using
foo: ->
with the object that has its method invoked being part of the environment. And why the SomeClass
part of result? Because something
's state could have changed as well!