Bringing Closures to Java 5, 6 and 7

Need closures on your Java platform? There is no need to wait for Java 8!         


Hosted on 
GIT hub

Basically, this library gives you the ability to strictly define a one method interface on the fly.

It also supports passing of non-final variables, voids, unlimited number of parameters, and prematurely breaking from a loop.


Closure.java

It is a one file implementation, for usage simplicity and works as a class container with public and hidden private classes. I have supplied three Closure.java implementations (actually made a couple more but were dismissed)
Most simple to use and understand version is v0but my favourite of the three is v1 though, but have a look at v2 as well which is a bit more dynamic, whereas v0 and v1 are fully static.


Examples

To run the examplescheck out the entire project, open a command prompt, standing at the root directory (where org, and examples directories are) and run:

>> javac examples/v1/ClosureExamples.java
>> java  examples.v1.ClosureExamples





Contribute?

If you are interested in helping out with shaping this library, the utility library Lib.java, improve the implementation, or provide more versions just sent me an email at mohamed.seifeddine _at_ gmail dot com and I will grant you access to the repo.


Functional Java

Ok, some of you might suggest that this already exists in Functional Java. This isn’t true. Focus there seems to be on doing things like sort, filter, exits, map, foldRight, left and what not.

When you want more than those predefined, it is hard to figure out how to do something like this implementation offers.

Same thing applies to the libraries of Guava and Lambdaj. Their focus are not on bringing closures to the Java world, but more on functionality similar to Functional Java.


Relevant

Intellij IDEA is preparing for the project lambda, so it will be easy to convert the above to real lambdas:
Intellij will also, already from today show the Closure in the editor in a more compact mode.

In Intellij 12 this piece of code:






Is by default minimized as a compact Java 8 lambda:



Project lambda

Current syntax suggestion for the project lambda is pretty ugly, and hard to understand. There are too many ways a lambda/closure can be defined, it should be more coherent and consistent which will make it hard for novice developers to adopt its usage I am afraid. http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html


I kind like how it looks like on this old presentation I found. http://www.slideshare.net/enbohm/project-lambda-4599885

10 comments:

  1. Cool! Exactly what I have been looking for!

    ReplyDelete
  2. A bit verbose but incredibly powerful! Nice effort!

    ReplyDelete
  3. Very nice! Yes, as someone else said, it's verbose, but Java is verbose sometimes. I'm really looking forward to lambdas in Java 8! Look me up and introduce yourself sometime as I don't immediately see how to find out who you are.

    ReplyDelete
    Replies
    1. It is as thin as it can be using Java.
      With Java 7, there is also generic type interference on the right hand side on instance creation so there is no need for repeating the generic part.

      Also, using import static org.mo.closure.*;

      we could also do

      new R1<>(){} rather than new Closure.R1<>(){ } everywhere.

      Delete
  4. http://functionaljava.org/

    ReplyDelete
    Replies
    1. Yes, I have seen the functionaljava library before, and initially this blog post contained a reference to it.

      Have you seen the size of that library, any idea of how to actually use it? It tries to do so much more than just provide Closures to the Java world, yet fails to provide the most basic.

      http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/index.html

      Delete
    2. functionaljava.jar 1,339kb

      Delete
    3. Size meaning number of classes and too many methods. Functional java tries to be functional, but really I just want Closure support. Java compiled class files is not the way to measure that :)

      Functional java focus seems to be on doing things like sort, filter, exits, map, foldRight, left and what not.
      When you want more than those predefined, it is hard to figure out how to do something like this implementation does.

      This one supports passing of non-final variables, voids, unlimited number of parameters if you want.
      Functional java is intended for something else, I am not sure it even provides "Closures" or something similar as this to the java world.

      Could you show me how I could do an eachLine on a file, eachFileRecure, findFileRecurse that takes a "Closure"? I am unable to figure that part out.

      I cant even figure out which class is actually supposed to provide something similar as the implementation here.

      http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/data/vector/V4.html

      http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/P5.html

      http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/F4.html

      http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/overview-summary.html

      You know, the car might already be invented, if that is even the case here(?), but they still build new and better ones.

      Delete
  5. Why 20 as the number of maximum static parameters before you move on using varargs?

    ReplyDelete
  6. Yes you are right! This should probably be higher, but I didn't want to push it to far either. What do you think is a better number?

    ReplyDelete