Thu 17 Jun 2010
I’ve just been playing with the first fruits of the Lambda project. It takes a bit of messing around to get going; you need to downgrade the version of HotSpot in the lambda repository to b93 as so:
$ hg fclone http://hg.openjdk.java.net/lambda/lamdba $ cd lambda/hotspot $ hg update -r jdk7-b93
Once done, you can build code using closures like so:
$/mnt/builder/lambda/j2sdk-image/bin/javac -XDallowFunctionTypes Closures.java
I found that it’s pretty easy to implement a simple version of map which takes a collection of objects and a function to produce a collection of the results:
public static <S,T> List<S> map(List<T> s, #S(T) func)
{
List<S> list = new ArrayList<S>();
for (T t : s)
list.add(func.(t));
return list;
}
public static void main(String[] args)
{
#Integer(Integer) square = #(Integer x)(x * x);
List<Integer> list = Arrays.asList(new Integer[]{4,5,6});
List<Integer> squares = map(list, square);
System.out.println(list + " squared is " + squares);
}
This can be run as follows:
$ /mnt/builder/lambda/j2sdk-image/bin/java Closures [4, 5, 6] squared is [16, 25, 36]
Sadly, the current implementation doesn’t like autoboxing too much. javac crashes if you pass an Integer into a function requiring an int, and you need an #Integer(Integer) function for the above; javac throws an error if you try to use a #int(int).
-
Ron
-
http://nabeelalimemon.blogspot.com Nabeel Ali Memon
-
Andy Tripp
-
http://mulletron.uwcs.co.uk/blog/ Richard Warbrton
-
mike
-
http://www.gnarc.com/sphere/higher-level-functions%e2%80%a6-in-java Higher Level Functions… in Java! | Gnu Architecture
-
http://developer.db4o.com German Viscuso
-
http://fuseyism.com gnu_andrew