r/programming • • Sep 19 '16

Is your JavaScript function actually pure? Or does this function behaves like a Mathematical Function in all the situations I will encounter in my code?

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

15 comments sorted by

12

u/Baaz Sep 19 '16

This is nonsense. The examples don't break the prerequisite of pureness:

every time you pass the same inputs, you always get the same outputs

Who cares if the output is null or undefined or NaN? Same input, same output, by your own definition. So this doesn't break it at all.

Also, the example of assigning random values to a parameter is BS, because now you're changing the input. Different input, different output, still doesn't break the rule for pureness.

4

u/_jk_ Sep 19 '16

indeed the author is conflating impure and partial functions,

1

u/fagnerbrack Sep 19 '16

How would partial functions fit into this context? Can you elaborate, thanks.

1

u/_jk_ Sep 20 '16

in the sense that, for a statically typed language, if a function throws an exception it is not a total function.

for dynamically typed its a little less clear, in that you really have a single static type of which exceptions are a member, so in some sense all function are total (but not in a useful way)

either way as u/Baaz points out you are not discussing purity any more but something else

1

u/Veedrac Sep 20 '16

It's entirely valid to consider exceptions as a result if you're in a language that allows you to catch them. A function is only strictly non-total if it never terminates.

In contrast, exceptions are side-effects so do break purity.

1

u/_jk_ Sep 21 '16

head is the usual go to example of a partial function, and this throws on an empty list. I don't think I agree that head is total just because it is possible to catch the error it throws.

likewise head is considered to be pure even though it can throw, so throwing exceptions does not break purity. catching exceptions on the other hand is impure

1

u/Veedrac Sep 21 '16

You'll probably find that languages which advertise a throwing head as pure don't allow catching them. For example, in Haskell "an undefined value cannot be distinguished from an infinite loop". This is not the case in Javascript.

1

u/fagnerbrack Sep 19 '16

Who cares if the output is null or undefined or NaN? Same input, same output, by your own definition. So this doesn't break it at all.

I agree. If it throws an error with input x and if you pass the input x again it keep throwing an error then it has the same behavior, it would be "referential transparency" in a slightly different way from what pure FP suggests even if it doesn't really "return" anything. I guess the author considers "pure" in JavaScript a function that doesn't throw, because for the sake of the FP argument a function is not supposed to "throw" any exception.

Also, the example of assigning random values to a parameter is BS, because now you're changing the input. Different input, different output, still doesn't break the rule for pureness.

But, in this case, it stops being idempotent and therefore loses it's referential transparency attribute if the input changes its underlying implementation for a Math.random() value.

3

u/Space-Being Sep 19 '16

But, in this case, it stops being idempotent and therefore loses it's referential transparency attribute if the input changes its underlying implementation for a Math.random() value.

Referential transparency implies idempotence, but not the other way around. You can have a function that is idempotent but not referential transparent.

function idempotent(value) {
    myGlobalVar = 0
    return value + 10;
}

1

u/fagnerbrack Sep 20 '16 edited Sep 20 '16

Probably silly question: why is that function not referential transparent?

function idempotent(value) {
    myGlobalVar = 0
    return value + 10;
}

=>

idempotent(10) // => 20
20

I can replace with its corresponding value as long as the side-effects are irrelevant (nothing is using the myGLobalVar), and even if something else is using a variable idempotent creates, it won't alter the behavior of the program, just make it static.

Maybe I am missing some definition of referential transparency for this specific example?

1

u/Space-Being Sep 20 '16 edited Sep 20 '16

But there is a side effect. And by considering idempotent you do not know if myGlobalVar is irrelevant - almost all globals are relevant. Just to expand on this, if myGlobalVar was never ever read after any call to idempotent it could still be referentially transparent by the definition in the bottom - but then it is in effect a local. Consider:

console.log(idempotent(10));
...
...
if (myGlobalVar == 0) {
    //Do something
}

This would log 20, and "do something". However, if we can view idempotent as referentially transparent we are allowed to replace it by the result:

console.log(20);
...
...
if (myGlobalVar == 0) {
    //Do something
}

This would alter the behaviour.

Maybe I am missing some definition of referential transparency for this specific example?

The typical definition I use and come across is the same as on Wikipedia:

... An expression is said to be referentially transparent if it can be replaced with its corresponding value without changing the program's behavior.[1] As a result, evaluating a referentially transparent function gives the same value for same arguments. Such functions are called pure functions. ...

Another, simpler, example is simply incrementing a global variable as a side effect, but that is clearly not idempotent.

1

u/Veedrac Sep 20 '16

That's only if exceptions do not count as a side effect, but that's not really reasonable because they nonlocally affect control flow. I suppose you could conceptually catch the exception before leaving any function, hoist it into the function return value, reread it from the call site and then rethrow it, which might make the function technically "pure", but that's just semantic relabelling of who owns what effect - it doesn't solve the real question.

Even if it did, that still doesn't really solve things because the function accesses global mutable state.

2

u/Veedrac Sep 20 '16

A pure function also has to be side-effect free. Exceptions are a side-effect, ergo a throwing function is impure. This validates the first example.

The second example does break purity. The sum function is causing global state changes through valueOf and the random seed.

1

u/fagnerbrack Sep 19 '16

This is a x-post from /r/javascript.

4

u/jms_nh Sep 19 '16

OP is wrong; a mathematically pure function acts upon plain values identically. By screwing around with valueOf in Javascript, you are creating 'impure' behavior but that's the fault of the argument to the function, not the function itself.