Groovy - Ruby for the Java crowd, with free semantics!

Luca Foppiano demonstrating Groovy features at Fosdem 2010

Groovy looks a lot like Ruby, and its added semantics provide a free hook for parallelization.

Today at Fosdem, I attended a talk from Luca Foppiano on Groovy. I knew Groovy is a dynamic language, but I wasn't aware that it's this much like Ruby.

All valid Java code is also valid Groovy code, so it is easy to phase in Groovy in you project. You can just leave stuff out, and get the free benefit of language features like type inference and closures.

The latter is quite interesting. Say, you want to select a subset of a list in Java, you use something like

List result = new ArrayList();
for (MyObject o: someList) {
  if (o.hasRightProperties()) {
    result.add(o);
  }
}

while in Groovy one might use

someList.select{ | o | o.hasRightProperties() }

Apart from being shorter, the Groovy form has better semantic properties: it guarantees we will be checking every element of the list, there is no way to break out of the iteration. Hence, we can freely parallelize this iteration, and move half of it over to another processor!

The means for this are already present in the language: make the iterating functions aware of their parallelizability, instead of changing the semantics of the for as a language construct. Now that's is a free benefit to me!