Closures in valid Java, limited to single expressions

Håkan Råberg hraberg at yahoo.se
Fri Apr 2 03:35:59 PDT 2010


I'm one of those who still hold out for closures in Java.

While waiting, I've implemented a port of Ruby's Enumerable module to Java, which supports simple single expression closures, generated using ASM, as part of the library:

  int factorial = inject(ints, fn(n, m, n * m));

  Map<String, Integer> map = new HashMap<String, Integer>();
  eachWithIndex(strings, fn(s, idx, map.put(s, idx))); 

  int x = 6;
  fn(n, x += n).call(2);
  assert x == 8;

I do realize that what I've implemented here is far from what has to be done to really add closures to the Java language, and that there are loads of cases that the different proposals address that this obviously don't, and can't, as it has to work with current versions of Java.

Having said that, I'm interested in some feedback from this group.

The project lives at:
http://github.com/hraberg/enumerable

Some background:
http://www.jroller.com/ghettoJedi/entry/closures_in_valid_java_limited

cheers, Hakan


__________________________________________________
Använder du Yahoo!?
Är du trött på spam?  Yahoo! E-post har det bästa spamskyddet som finns 
http://se.mail.yahoo.com 


More information about the lambda-dev mailing list