Thursday, September 23, 2010

Lamdbas in Java 1.6

If you've ever heard that java is a poor language because it doesn't have lambdas, that ends today!

I created a way to do lambda's in java 1.6 without changing: the JDK, the compiler, the ByteCode or the Syntax.

I vBlogged my thoughts here:

It's only a jar file, no special requirements or changes.

You can down load it here at

You might also be interested in my similar addition of extension method in java

Here's the diagram of the syntax :

I also made a small tutorial here :


Brian Maso said...

I like the "{{...}}" trick -- about the simplest way to have an anonymous code block.

One downside is that the closure must be run at least once, when the Function object gets created initially. Not a dealbreaker, but definitely a quirk of which to be aware!

Razie said...

Nice! I think it also does a great job explaining lambdas to non-functional folk...and a whiff of closures.

Scott said...

Those are anonymous methods not lambdas. Lambdas are just a syntax thing derived from lambda calculus.
That shouldn't in any way diminish from the coolness of what you are showing though :)

Christian Gruber said...

Um... doesn't seem to contain Lambdas at all. Did you mis-upload, or am I missing something.

Christian Gruber said...

Totally awesome syntactic sugar, though.

Llewellyn Falco said...

My mistake, forgot to modify the build script.

Lambda.003 works

Allan said...

Nice little trick for creating something akin to continuations, and a great explanation why you would want to pursue it. However, it has very little to with lambda calculus.

mario said...

Hi Llewellyn,

I'm Mario, the creator of lambdaj, and we met at JavaOne. Thank you for sharing this. It's a really smart trick and I'm already playing with it :)

John Yeary said...

Hello Llewellyn,

Thanks for the email with the update.

It was great to meet you at JavaOne, and have you actively participate in the Java User Group (JUG) Sunday on the discussions on: "Languages on the JVM", "Future Enhancements to Java", and "If I were King/Queen for a Day... What would I change in Java".

I am very glad and proud that the Sunday discussion got your creative juices flowing.

Your initial implementation demo to Sven Reimers, Martin Klähn, and I was cool, but as I noted: your modification of Object creates an extension long tail. I am glad to see that you took it to heart and made changes to your current API design.

You are incredibly bright, and this is a great piece of syntatic sugar to get "Closures" in JDK 6.

Thanks for sharing this with the rest of us. Duke was impressed!

kinko said...

Would be nice if it actually worked. All I get is the below.

Also you example code picture is syntactically incorrect.

Exception in thread "main" java.lang.Error: java.lang.NoSuchFieldException: this$0
at com.spun.util.ObjectUtils.throwAsError(
at org.lambda.functions.Function.getParentThisReference(
at org.lambda.functions.Function.initalizeParameters(
at org.lambda.functions.Function.getParameters(
at test.Test.main(
Caused by: java.lang.NoSuchFieldException: this$0
at java.lang.Class.getDeclaredField(
at org.lambda.functions.Function.getParentThisReference(
... 5 more

Llewellyn Falco said...

@kinko thanks, I didn't notice the photo had some extra )'s it fixed now.

Also, I wasn't able to completely reproduce the bug you had. I think it's because your compiler is creating a different default name for the parent class.

But I faked it by using a different name, and fixed it for that scenario. The code is posted in Lambda.006 try it out and let me know if it works.

Again, thanks for submitting feedback. My code is unit tested, but I can only test on my own system. Too often when things don't work, no one tells me, and the problems never get solved.