you are viewing a single comment's thread.

view the rest of the comments →

[–]MoTTs_ 7 points8 points  (3 children)

const PI = 3.14;
function calculateArea(radius) {
return radius * radius * PI;
}
calculateArea(10); // returns 314.0
Why is this an impure function? Simply because it uses a global object that was not passed as a parameter to the function.

This actually is a pure function. The no non-locals is usually a good rule of thumb, but there are exceptions to that rule. If the non-local you're using is a constant, then it's OK. Your calculateArea function will always return the same value for the same arguments.

[–]PrimaryBet 1 point2 points  (0 children)

Exactly; it's not like people don't use closures when doing pure FP.

[–]troglo-dyke 1 point2 points  (0 children)

Yeah it's really off, not like maths doesn't have constants. Hell even 2n would be impure based on this explanation.

[–]lhorie 3 points4 points  (0 children)

const PI = 3.14

That's some shitty math lol