Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 310
bbnn Avatar asked Sep 14 '16 18:09

bbnn


People also ask

Can pure functions make API calls?

Yes you can do the same to Math. random() if you memoize it making every call to pureRandom return exactly the same number. And this would make pureRandom pure because it always returns the same result. I personally would not call it a "random" function.

Can a pure function return a function?

A Pure Function is a function (a block of code) that always returns the same result if the same arguments are passed. It does not depend on any state or data change during a program's execution. Rather, it only depends on its input arguments.

Is it is possible to call a function with in a function?

Calling a function from within itself is called recursion and the simple answer is, yes.

How do you know if a function is pure?

In computer programming, a pure function is a function that has the following properties: the function return values are identical for identical arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams), and.


1 Answers

When you can

A pure f function can call any other function/method g0...gn. But 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.

like image 197
Luca Angeletti Avatar answered Sep 28 '22 03:09

Luca Angeletti