Peel me a Grape :: We make things work

We Make Things Work :: Blog

I wanted to check out the Java Closures Prototype , but when I downloaded it I found that it had a few shell scripts (javac, java, javadoc) that you should call instead of the jdk ones. I found this annoying, so I set up some ant to do it instead.

So, https://please.peelmeagrape.net/svn/public/java/closures/closures_ant.xml is an ant script that you can include:
    <import file="closures_ant.xml"/>
And then, if you call the ‘setup_closures’ target, it will download and extract the prototype, and override the definitions of the javac, java and javadoc ant tasks to use the closures prototype:
    <target name="build" depends="setup_closures">
        <mkdir dir="class"/>
        <javac srcdir="src" destdir="class" debug="true"/>
    </target>

Note: You will need java 1.6. On OSX, you can download this from The ADC . This will not change the system default jdk, so you need to put something like this in your .antrc:

JAVACMD=/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Commands/java

With that all done, you can play with the closures prototype proper. I have a project in svn that uses this ant script. I have pasted in most of the source code from the Closures tutorial by Zdeněk Troníček so you can compile and run it (using ant tutorial). The ant decompile target will decompile (using jad) the code generated by the prototype on windows or mac, so you can see what kind of stuff it’s doing under the covers.
svn co https://please.peelmeagrape.net/svn/public/java/closures/ closures
cd closures
ant tutorial
So, now you can do stuff in java like (EnumerableStuff.java ):
List<String> words = Arrays.asList("The quick brown fox jumped over the stupid dog".split(" "));

System.out.println(map(words,{String s=> s.length()}));
System.out.println(select(words,{String s=> s.length()<=4}));
System.out.println(first(words,{String s=> s.length()>5}));
giving you:
[3, 5, 5, 3, 6, 4, 3, 6, 3]
[The, fox, over, the, dog]
jumped

Hopefully java 7 will see this and extension methods , and then I can stop feeling like I’m continuously compiling 1 line of ruby in my head into 6 or 7 on the keyboard!

blog comments powered by Disqus