Can a pure function call external function?
Asked Answered
B

3

12

Can a pure function call an external method?

for example:

class Dog {
  function jump(name) {
    return "a dog named " + name + " jumped!"
  }

  function jumpTwice(names) {
    var result = [];
    for (var i = 0; i < 2; i++) {
       result.push(jump(names[i]));
    }
    return result.join("\n");
  }

}

can jumpTwice() be considered as a pure function?

Bouie answered 14/9, 2016 at 18:57 Comment(0)
V
23

When you can

A pure function f can call any other function/method g0...gn. However, g0...gn must be pure as well.

When you cannot

As soon as you get a pure function f and you invoke a non pure function g from within f, then f is no longer pure.

Veratrine answered 14/9, 2016 at 19:12 Comment(2)
But jumpTwice depends on what jump does. If the definition of jump changes, then the behaviour of jumpTwice changes. Doesn't it violate the "same input, same output" rule for pure functions?Insatiate
Pure functions are not supposed to guarantee the same output if you change the codebase and rebuild the project.Veratrine
M
0

Yes, it is pure. It doesn't mutate any global state nor gives different results for same input.

Missend answered 14/9, 2016 at 19:2 Comment(0)
A
0

In the specific case you give, yes. But what you have there is a method and methods require extra care.

Pure functions always give the same result for any given input, no matter what the current program state.. Methods can be viewed as functions which are passed their object as a hidden parameter. To be pure, the method must not access any implicit state (nor may they call any other methods/functions which are susceptible to implicit state). This means also not using any object fields which contain implicit state. The comments discussion hopefully gives an example of how to judge that context.

It's not enough to shun mutable fields - the value in immutable fields field must be knowable/predictable . For example, if an object contained an immutable private field which was given a random number on object creation, any method which used that value to compute it's output would be impure.

On the other hand, if your Dog class had an immutable name field which was set on object creation, then methods which used that field can be considered pure (unless something else disqualifies them).

EDIT

It would have been helpful to say that "side effect" and "purity" is contextual (as discussed in the comments). Which is what I didn't entirely clearly allude to by the use of the words "knowable" and "predictable". The comments discussion illustrates the importance of knowing the context.

Apology answered 14/9, 2016 at 21:18 Comment(11)
I'm not sure what you mean by "knowable/predictable". All fields that are part of the object contribute to the input of the method. That the object creation is impure (involving randomness) does not make the method itself impure.Xylotomous
If the method ignores stateful properties of the object then it is predictable and pure. No matter what the mutable parts of the input, in that case, the output is predictable.Apology
@Xylotomous to pick the most simple example, if a class method always returned the value 1 in any instance, it would be pure no matter how mutable the object's fields.Apology
The point I'm trying to make is that it does not need to ignore the mutable properties. It just must not alter them, it can access them without making anything impure.Xylotomous
If it uses mutable values to compute the output, it isn't pure.Apology
No, it's pure as it does not mutate them. As properties of the object the method is called on, they are explicit input (and constant during the execution), not implicit mutable state.Xylotomous
I guess this breaks down to our definition of equality. Is an object in its old state equal to the same object with mutated properties? I say no, and then such a mutable object can be the input of a pure method.Xylotomous
By your argument, creating that object with the hidden random field and then calling a method which returns its value is a pure operation. Which absolutely is not true. Purity depends on context not least because "side effect" is only definable given a clear context) but that's precisely why I emphasised care with class methods.Apology
No, creating the object with a hidden random field is not pure, only calling the method that returns the hidden field on the object is. And together, they are obviously impure.Xylotomous
Yes, if you're precise about the context then I concede your point.Apology
As a good example of a functional OO I tend to point at the String class in Java. All the methods are pure and an instance never changes its state.Mayberry

© 2022 - 2024 — McMap. All rights reserved.