loading...

re: The beauty of Functional Programming VIEW POST

FULL DISCUSSION
 
 
[deleted]
 

Well, it is a pure function.
What is wrong with accessing variables from function scope?

Because it is performing a side effect, by definition, can you write me a example using interation that you agree that is a impure function?

Sure

let result = 0

function myCount() {
  for (let i = 1; i < 10; i++) {
    result += i
  }
  return result
}

There is example of doing side effects by modifying outside scope, which as you stated, makes the function impure.

In your example you aren't accessing "outside" state

 

Sorry Fanny, but it would be classed as a pure function in spite of writing to a local variable. As long as the write has no side effects that are externally observable, it would satisfy the definition of a pure function.

Even a recursive function with no visible variable assignment may introduce any number of intermediate variables as part of compiler optimisation; this wouldn't make it any less pure provided these changes did not escape outside function scope.

code of conduct - report abuse