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

13

u/reeferd Aug 27 '16

If the input to a function always is random, then the input is never the same - or am I missing something here?

5

u/redditBearcat Aug 27 '16

That part was kind of confusing. I think he was demonstrating that even in cases that you're sure a value is actually constant, someone else could have tampered with the way the underlying Javascript behaves.

ie. There's no way to be sure of anything in Javascript.

2

u/[deleted] Aug 27 '16

There's no way to be sure of anything in Javascript.

But isn't this true for almost any language? Great examples are C/C++ with pointers and stuff like that and Python.

1

u/redditBearcat Aug 27 '16

I certainly can't speak for all other languages. I think his point was also that other languages provide better ways to validate the input to your function. Which is why he mentioned haskell at the bottom.

1

u/ChronoChris Aug 27 '16

assignment and calculation of the Math.random should be done when you instantiate math.random to the variable. It shouldn't be every time the multiplication is ran on the variable.

1

u/vinnl Aug 28 '16

The input is the function, and the function is the same every time. You could have a pure function that takes another function as input, then doesn't execute it but e.g. decorates it and then returns it. Even if the function you pass in isn't pure itself, the function that it is passed into is pure - every time you pass in that same function, you'll get the same decorated function as output.