3
I started studying functional programming and I’m a little confused about these two items.
My point is: Java lambdas are equivalent to Javascript Closures? If they are not, what is the difference between them?
Thank you.
3
I started studying functional programming and I’m a little confused about these two items.
My point is: Java lambdas are equivalent to Javascript Closures? If they are not, what is the difference between them?
Thank you.
4
In the general concept they are yes, but there are different details.
Behold: What is 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 enclose variables, so they are closures, and both use the simplified syntax of lambda, especially the newer versions of Ecmascript. The old syntax was more of an anonymous function. Before Java 8 there was no such feature, nothing like it, just could achieve the same result with a good extra work, all manual, the language did not help.
Both are functions that are linked 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. There need be local variables.
The way the capture of variables and in which 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 the same, then there are no guarantees and in some cases it is certainly different.
"Both use the simplified lambda syntax, especially the newer versions of Ecmascript. The old syntax was more for anonymous function". Lambdas and anonymous functions are not (exactly or roughly) the same thing? (in Javascript, at least, since in Java Ambdas would be anonymous classes disguised)
more or less yes, exactly no.
1
A lambda is just an anonymous function. A function that is defined without a name. And closures is, according to the Wikipedia:
A closure usually occurs when a function is declared within the body of other, and the inner function references local variables of the outer function.
That is, you have a function defined within another function the internal function uses parameters and variables of the external function.
Put the direct link to Wikipedia
Thanks! I found it very interesting to navigate to Wikipedia in English and see the etymology of the word closure
Browser other questions tagged javascript java lambda-expressions functional-programming closures
You are not signed in. Login or sign up in order to post.
I think this response in the international OS helps : https://stackoverflow.com/a/220728
– Jefferson Quesado
Note that in Javascript there is also Arrow Function that although very similar the "Anonimas functions" still have one or another different behavior, as the example where answered: https://answall.com/a/206881/3635, I have not yet detailed the answer well, because I have not found a legal source to use of reference, but I intend to edit it.
– Guilherme Nascimento