0

Using a value in the nested (Closure like) function such as:

const f1= () => {
    const a = 1;
    const f2 = () => a;
    return f2;
    };

f2 does not have arg of a, but returns a of the upper-scope of f1.

  • It is functional and you're using closure the functional way too. You should post this on codereview.stackexchange.com instead though. – Randy May 24 '16 at 05:46
  • Relevant https://en.wikipedia.org/wiki/B,_C,_K,_W_system (see K-combinator) – zerkms May 24 '16 at 05:49
  • @randy Why do you recommend posting on Code Review? There is no reviewable code here. Please read [_A guide to Code Review for Stack Overflow users_](http://meta.codereview.stackexchange.com/q/5777). – 200_success May 24 '16 at 05:49
  • sorry, I simply forgotten to add the return value of f1. I modified the ode. –  May 24 '16 at 05:49
  • @200_succes because the question is open for comments on any part of code, it is not a question closed Down to one answer and the user asks for improving answers, not a solution – Randy May 24 '16 at 05:52

1 Answers1

2

Yes.

a is a constant and referentially transparent. It doesn't matter that f2 is a closure as long as it does not close over mutable state.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks Bergi, but I don't like to use a buzz-word "referentially transparent", many argue on this term and disagreement there. Can you view in other way? –  May 24 '16 at 05:56
  • `a` is referentially transparent by all definitions of the term :-) About what disagreements are you concerned specifically? – Bergi May 24 '16 at 05:59
  • http://stackoverflow.com/questions/4865616/purity-vs-referential-transparency?rq=1 and I agree to the answer#1, the word "referentially transparent" is not useful, and personally, I avoid to explain using that term in FP context. –  May 24 '16 at 06:30
  • I'm not terminology-versed enough to find a better term, but I think that [any of these definitions](http://stackoverflow.com/q/210835/1048572) works for `a`. – Bergi May 24 '16 at 07:01
  • Speaking of terms: Wikipedia states that [purely functional languages](https://en.wikipedia.org/wiki/Purely_functional) guarantee the equivalence of call-by-name, call-by-value and call-by-need evaluation. JS pursues only the call-by-value strategy. So does **purely functional** in the context of JS just mean **pure functions**? –  May 24 '16 at 18:41
  • @Iven Yes, pure means pure, because the actual evaluation strategy does not matter. – Bergi May 24 '16 at 19:38