Tuesday, December 29, 2009

Syntax Hell

As anyone who has applied a more functional style to Java will realize, the Java syntax really gets in the way. I was using my usual guinea pig, JRDF, with "with" so you don't have to iterate but apply a function and so you don't have to close a ClosableIterator. The typical code to print out the graph is:

ClosableIterable<Triple> triples = graph.find(ANY_TRIPLE);
try {
for (Triple triple : triples) {
System.out.println("Graph: " + triple);
}
} finally {
triples.iterator().close();
}


Using "ClosableIterators.with" this becomes:

with(graph.find(ANY_TRIPLE), new Function<Void, ClosableIterable<Triple>>() {
public Void apply(ClosableIterable<Triple> object) {
for (Triple triple : object) {
System.out.println("Graph: " + triple);
}
return null;
}
});


It's typically one line less but that's not that much of an improvement.