Re:Java 7: Oracle pushes a first version of closures
by bnoordhuis on Monday May 31, 2010 @11:46AM
(#1392730)
The syntax won't win beauty pageants but I like how they retrofitted lambdas onto interfaces so you can do something like:
[ Parent
| on HN ]
Re:Java 7: Oracle pushes a first version of closures
by lzimm on Monday May 31, 2010 @12:09PM
(#1392806)
dude, you made it look so much prettier than those examples
Re:Java 7: Oracle pushes a first version of closures
by jpr on Monday May 31, 2010 @12:38PM
(#1392887)
(sort list #'string-lessp) still wins in my book :)
Re:Java 7: Oracle pushes a first version of closures
by jimbokun on Monday May 31, 2010 @01:38PM
(#1393023)
You can always static import Collections.sort. The main thing missing, then, is a way to simply refer to an existing method to perform the comparison. Is there syntax for this in the current release? (I believe there was in some of the earlier proposals.)
Re:Java 7: Oracle pushes a first version of closures
by tlrobinson on Monday May 31, 2010 @02:40PM
(#1393148)
I haven't tried this but according to the comments on the post you can do something like this:
Comparator string_lessp = (Comparator) #(String a, String b) {
return a.compareToIgnoreCase(b);
}
sort(list, string_lessp);
Re:Java 7: Oracle pushes a first version of closures
by jpr on Monday May 31, 2010 @03:22PM
(#1393237)
Yeah, but that just seems wrong. It's like "I have this function, but I can't actually refer to it, so I'll have to wrap it in this thingamajig just so that I can pass it around".
Re:Java 7: Oracle pushes a first version of closures
by tlrobinson on Monday May 31, 2010 @03:41PM
(#1393286)
Huh? You can, but the sort method already takes a Comparator class. If it took a function you could pass around the function instead.
Anyway, the point was only that "(sort list #'string-lessp)" is nothing special.
Re:Java 7: Oracle pushes a first version of closures
by jpr on Monday May 31, 2010 @03:49PM
(#1393314)
The definition of Comparator-interface includes only two methods, .compare and .equals. The documentations says that it is always safe to not override .equals, so we're left with just .compare. This effectively means that each comparator is effectively a function of two arguments that returns a boolean. Had functions been in Java in the first place, the whole Comparator interface would have been superfluous.