r/javascript • • Aug 27 '16

Is your JavaScript function actually pure?

http://staltz.com/is-your-javascript-function-actually-pure.html
8 Upvotes

48 comments sorted by

View all comments

Show parent comments

3

u/hahaNodeJS Aug 28 '16

let x = [Math.random(), Math.random(), Math.random(), Math.random()];

This is not the same as his example. Here, you have a set of random values that you keep changing. In Staltz's example, he has a set pointing to the random function.

1

u/vinnl Aug 28 '16

Exactly. Pure functions can return different outputs if the inputs are different, which is the case here.

1

u/hahaNodeJS Aug 29 '16

Right, but you're not addressing the same point the author is making. If you pass in the function Math.random, the method should always return the same value. In the case for a function f(x,y) that sums x, y, the returned value should be 0 if either x or y are non-numbers.

In your example you're simply passing in different numeric values for x,y,....

1

u/vinnl Aug 29 '16

Yes, that was what I was saying - the example wasn't mine :)