Are Java lambdas equivalent to JavaScript Closures?

3

I started to study functional programming and I'm a bit confused about these two items.

My question is: Is Java lambdas equivalent to JavaScript Closures? If not, what is the difference between them?

Thank you.

    
asked by anonymous 25.05.2017 / 18:22

2 answers

4

The general concept is yes, but there are different details.

See: What's the difference between a lambda expression, a closure and a delegate? .

With this understand that there are several names for the same or almost the same thing.

Details:

In both languages they lock variables, so they are closures , and both use the simplified lambda syntax, especially newer versions of EcmaScript. The old syntax was more for anonymous function. Before Java 8 did not exist this feature, or anything like that, it only gave the same result with a good extra work, everything manual, the language did not help.

Both are functions that are tied to variables anywhere, including parameters and function returns. They can be called through these variables.

There is a myth that they need to be within other functions. They only need to be assigned to a variable, even indirectly. They do not have to be local variables.

The way in which catching variables and in what situations this is possible changes. Especially me variables that are not local.

If you want to know if they do everything the same, if they produce exactly the same result using equal, there are no guarantees and in some cases it certainly is different.

    
25.05.2017 / 18:36
1

A lambda is just an anonymous function. A function that is defined without a name. And the closures is, according to the Wikipedia : p>

  

A closure occurs normally when a function is declared inside the   body, and the internal function references local variables of the   function.

That is, you have a function defined inside another function the internal function uses of parameters and variables of the external function.

    
25.05.2017 / 18:29