Pure(?) function - same input, different outputs ๐
Kind of a 'trick' - I know - but the input is identical each time. Because of this, in the strictest sense, it can be considered that functions such as these are not pure - as there are possible inputs that will wreck the purity.
But the input to the function is the same. The calculation differs because of coercion that happens as a feature if JS. The inputs themselves are unchanged. The input in this case is an object with a single method.
The input is an object, but it's value upon coercion to a number differs on each coercion. In a very strict sense, such a simple function could be considered impure. To make it strictly pure in JS, you could add a bunch of code checking types etc.
Put simply, the input itself is impure. The valueOf getter uses performance.now which is external state (calling valueOf with the same inputs does not return the same result every time). When you introduce impure inputs then youโve already broken the fp paradigm.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
JS is fun...
Pure(?) function - same input, different outputs ๐
Kind of a 'trick' - I know - but the input is identical each time. Because of this, in the strictest sense, it can be considered that functions such as these are not pure - as there are possible inputs that will wreck the purity.
JS is fun, since input is changing its value on each call, hence output is different
But the input to the function is the same. The calculation differs because of coercion that happens as a feature if JS. The inputs themselves are unchanged. The input in this case is an object with a single method.
In input of function is function that keeps changes its value, then that function can not be considered as pure function, right ?
The input is an object, but it's value upon coercion to a number differs on each coercion. In a very strict sense, such a simple function could be considered impure. To make it strictly pure in JS, you could add a bunch of code checking types etc.
Put simply, the input itself is impure. The valueOf getter uses performance.now which is external state (calling valueOf with the same inputs does not return the same result every time). When you introduce impure inputs then youโve already broken the fp paradigm.