Home

News: Your first Java 8 Lambda Expression

  1. Your first Java 8 Lambda Expression (13 messages)

    Keeping up with the pace of a blog post a week I have wanted to take a closer look at Java 8 developer preview ever since it was release mid-November. Originally due to be in Java 7 but was dropped as part of the “Plan B” effort to help ship Java 7 earlier. Java 7 has now shipped and the focus is on the Java 8 which is due to be release late 2012. The syntax was finally confirmed after much debate by Brian Goetz on the lambda-dev mailing list and will adopt the C# syntax.  For more information on Lambda expressions in Java 8 check out the JSR-335

    Wait, let’s start at the start. What are lambda expressions and why oh why do we need them so badly that people fall out over the syntax.

    What is a lambda expression?

    Put simply a lambda expression is

       Lambda is an operator used to denote anonymous functions

    I just robbed that from Wikipedia, I surely can do better. In my words a lambda expression is

       A succinct way to represent an anonymous method

    My worst attempt at defining lambda expression was

       Anonymous classes, but much cooler!!!

    To find out how much cooler, read the full article: 

    http://diarmuidmoloney.wordpress.com/2011/12/10/my-first-java-8-lambda-expression/

    Edited by: Cameron McKenzie on Dec 12, 2011 12:58 PM

    Threaded Messages (13)

  2. Your first Java 8 Lambda Expression[ Go to top ]

    Your definition misses the point of lambdas. It's not a function pointer. Not a function alias.

    It's a function pointer + access to the local variables and parameters (the environments) of the enclosing function. That's the whole point and where the power comes from.

  3. When a function refers to a variable defined outside it, it's called a free variable. A function that refers to a free lexical variable is called a closure. (...). A closure is a combination of a function and an environment. Paul Graham, ANSI Common List, Prentice Hall, 1996, p.107.

    In other words a closure is an expression with a free variable, which points to an anvironment that may be in a scope >outside< of the closure (which cannot be done with inner classes, by the way).

  4. Rear Neil Gafter's blog[ Go to top ]

    I forgot to mention that Neil Gafter has a blog that explains closures very well, especially why inner classes in Java are not closures (which seems to be a never ending misconcection). Surf the Internet and you'll find it.

  5. Read Neil Gafter's blog[ Go to top ]

    I forgot to mention that Neil Gafter has a blog that explains closures very well, especially why inner classes in Java are not closures (which seems to be a never ending misconcection). Surf the Internet and you'll find it.

  6. Olli, I am not sure I have understood correctly your remark "which cannot be done with inner classes, by the way". In Java the inner classes allow access to variables declared in the outside scopes. (The local variables have to be final thoug, just as in Java 8 effectively)

  7. Olli, I am not sure I have understood correctly your remark "which cannot be done with inner classes, by the way". In Java the inner classes allow access to variables declared in the outside scopes. (The local variables have to be final thoug, just as in Java 8 effectively)

  8. There is closure and closure[ Go to top ]

    Some people estimate that a closure should let you modify the variables it closes on. This is not necessary to call something a "closure".

    A Java anonymous class has access to the (final) variables in the context it was declared and can be called (via a method), hence it is a closure. In a language like Java being able to modify external variables in the closure is desirable though. And the anonymous class syntax is just too heavy. 

  9. Reading Neal's post...[ Go to top ]

    I went and googled for Neal's article as you suggested. If I understand well, he believes a proper Java closure should deal with anything in scope, including methods and fields (ok), break/continue (not necessary IMHO) and return/throw (now this is controversial). 

    There is a discussion here: http://lambda-the-ultimate.org/node/2009

    My position is that anonymous inner classes are closures, albeit clunky ones. 

  10. Reading Neal's post...[ Go to top ]

    You posted your reply shortly before mine, so I didn't see it in time. The discussion followed by this article may be a good read: http://www.theserverside.net/discussions/thread.tss?thread_id=57148 Neal Gafter took some efforts there to explain the matter. I'm not into this another time. Actually, closures are more than 30 years old (Smalltalk-80 had them and Lisp before as well) and they are meanwhile well-defined ;-).

  11. Everything is relative[ Go to top ]

    Some people estimate that a closure should let you modify the variables it closes on. This is not necessary to call something a "closure".

    Sure, some people would also argue that a database that only allows for reads but not for writes is still a database ...

    A Java anonymous class has access to the (final) variables in the context it was declared and can be called (via a method), hence it is a closure.

    No, because a final variable is not a free variable, e.g. the expression with the final variable could not be passed on to some arbitrary object on the heap as the final variable is bound to the stack. That's the point. You can put a non-final variable into a final single element collection and this way you get round the problem. But this only adds to the code bloat of inner classes.

    Again, look for the articles of Neil Gafter about closures. They explain those things very well.

  12. Glad I learned LISP[ Go to top ]

    A few years ago I forced myself to learn LISP.  It looks like having an understanding of closures is really going to pay off.

    It's always a good language to know, but now even more so now that Lambdas are going to be in Java.

  13. I don't understand the example[ Go to top ]

    (Disclaimer: this post is unrelated to the prior discussion about closures and focuses instead on the original article)

    I don't understand the example in the original article. I "get" lambdas (I'm familiar with their use in functional languages), but I don't understand their purported benefits in Java; at least not in the example provided. I just see some fancy syntax for the lambda and then the same old verbose Java! If you have to define an interface, implement it with the lambda, and name the lambda, then what have we really gained?

    I would have expected truly anonymous lambdas being passed as function arguments, which the author doesn't show. Will this be possible?

    I do like the C# syntax for lambdas that the author mentions before his example.

  14. For more details on this topic[ Go to top ]

    Hi Guys,

    For more details on the Java 8 Closures - Lambda Expressions, synyax and workin examples please refer to my page at Introduction to Java Closures - Lambda Expressions with Examples.