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 www.approvaltests.com

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 : http://blog.approvaltests.com/2010/10/java-lambdas.html


13 comments:

  1. 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!

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

    ReplyDelete
  3. 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 :)

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

    ReplyDelete
  5. Totally awesome syntactic sugar, though.

    ReplyDelete
  6. My mistake, forgot to modify the build script.

    Lambda.003 works

    ReplyDelete
  7. 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.

    ReplyDelete
  8. 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 :)

    ReplyDelete
  9. 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!

    ReplyDelete
  10. 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(ObjectUtils.java:140)
    at org.lambda.functions.Function.getParentThisReference(Function.java:103)
    at org.lambda.functions.Function.initalizeParameters(Function.java:79)
    at org.lambda.functions.Function.getParameters(Function.java:65)
    at org.lambda.functions.Function.call(Function.java:37)
    at org.lambda.functions.F1.call(F1.java:13)
    at test.Test.main(Test.java:13)
    Caused by: java.lang.NoSuchFieldException: this$0
    at java.lang.Class.getDeclaredField(Class.java:1882)
    at org.lambda.functions.Function.getParentThisReference(Function.java:97)
    ... 5 more

    ReplyDelete
  11. @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.

    ReplyDelete
  12. Thanks for sharing this blog post,Nice written skill core Java online training

    ReplyDelete
  13. Budding coder here, - just thought id drop a line and say how helpful your blog has been on my journey into programming. I have found many tips and pointers here that continuously help me as i acquire more coding and programming knowledge. Thanks for sharing.

    ReplyDelete

Note: Only a member of this blog may post a comment.