functionfunctional-programmingconceptpure-function

Can a pure function call external function?


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?


Solution

  • 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.