What exactly is a pure function when we are talking about a function within a function

what is pure function in react
pure function vs impure function in javascript
what is pure function in python
pure function c++
pure function c#
benefits of pure functions
examples of pure and impure functions
pure functions vs idempotent

I've learned that a pure function is a function that doesn't alter global state, period. If this is true, functions within functions can alter the state of the outer function and still be pure, correct?

Example:

function func1() {
  let name = "My Name"
  func2()  

  function func2() {
    // alter name here.
  }
}

In the above example, func2 is still pure because it doesn't use any global state.

That's how I see, but my working colleagues believe that func2 is not pure, and it should be writen like:

function func1() {
  let name = "My Name"
  func2(name)  

  function func2(name) {
    // use name here.
  }
}

Which is bad, because:

  • if the v8 doesn't optimize this, the CPU will run more instructions
  • shadowing is a bad practice

The question is: What exactly is a pure function when we are talking about a function within a function?

Purity is not defined to care only about global variables, it cares about any non-local variables (and more) that shouldn't be mutated. An outer variable of a closure still counts as non-local, it doesn't need to be global.

So if func2 changes name, then it's impure. Whether func1 becomes impure as well through that depends on whether you consider only external purity - as long as name and func2 stay local within the function, it may still be pure.

What exactly is a pure function when we are talking about a function , Purity is not defined to care only about global variables, it cares about any non- local variables (and more) that shouldn't be mutated. An outer� What exactly is a pure function when we are talking about a function within a function I've learned that a pure function is a function that doesn't alter global state, period. If this is true, functions within functions can alter the state of the outer function and still be pure,

What exactly is a pure function when we are talking , I've learned that a pure function is a function that doesn't alter global state, period. If this is true, functions within functions can alter the state of the outer function� In programming, a pure function is a function that has the following properties: The function always returns the same value for the same inputs. Evaluation of the function has no side effects.

I've learned that a pure function is a function that doesn't alter global state, period.

Well, that's oversimplifying. A pure function should one, not have side effects, and two, its outcome should rely only on the arguments. So, as a corollary, no state. Your func1's name property suspiciously look like a state. Could I mutate it? Will func1() yield different results dependent of the previous calls? Impure!

Of course, func2 being impure is beyond dispute. You wrote "alters name" - 'name' is outside its scope. It's a side effect.

Pure function, In computer programming, a pure function is a function that has the following properties: Its return value is the same for the same arguments (no variation with � Pure Functions A function is called pure function if it always returns the same result for same argument values and it has no side effects like modifying an argument (or global variable) or outputting something. The only result of calling a pure function is the return value.

Another example I think it's worth mentioning is the follow pure function:

function insert(DB, user) {
    return function() {
        throwIfUserExists(DB, user);
        var savedUser = saveUser(DB, user);
        return savedUser;
    }
}

Note, that when you call insert, as long as you keep sending in the same DB and user, you will receive the same output as the function returns another function - no side effect happens.

What Is a Pure Function in JavaScript?, Pure functions are the atomic building blocks in functional programming. You get different results depending on when you called the function. Pure Functions A pure function is a function where the return value is only determined by its input values, without observable side effects. This is how functions in math work: Math.cos (x) will,

What's a Pure Function? - DEV, To use a function, you must define it somewhere in the scope from which little bit out of the way, let's talk about what exactly a pure function is. Pure function. A pure function is a deterministic function. This means when a same input is passed every time, the function will return same output. In mathematical terms it is nothing but a well defined function. A pure function will have the following properties. It depends only on its own arguments. It wont try to change variables out of its scope.

Pure Functions | Introduction to Functional Programming, A key tenet is making sure that your function is pure. 0:10 So we talk about the importance of pure functions in 0:13 the very beginning of this course. 0:16 And� The Wolfram Language lets you declare functions inline (called pure functions) to get around this. The most transparent way to define a pure function is with Function. The first argument is a list of arguments, and the second is a function. This function adds its two arguments together:

Functional Programming: Pure Functions, A pure function is a function where the return value is only determined by its input values, without observable side effects. This is how functions in math work: Math. cos(x) will, for the same value of x , always return the same result. A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. When a pure virtual method exists, the class is "abstract" and can not be instantiated on its own. Instead, a derived class that implements the pure-virtual method(s) must be used.

Comments
  • use != alter. Which one are you talking about?
  • @Bergi I would like to know your thoughts about both things.