On Thu, 2008-01-24 at 21:11 +0000, Paul Johnson wrote: [snip] > // Get the Foo that was most recently updated. > Foo latestUpdate (Iterator <Foo> iterator) { [...] > } > > This takes an iterator over some collection of Foos and finds the one > with the highest value of updateTime. 9 lines of code, or 12 with the > closing curly brackets. > > In Haskell this is so short and obvious you probably wouldn't bother > declaring it as a function, but if you did, here it is: > > -- Find the Foo that was most recently updated. > latestUpdate :: [Foo] -> Foo > latestUpdate foos = maximumBy (comparing updateTime) foos > > Of course you could always write it in point-free format, but I think > that would be over-egging things.
To have a fair comparison you'd have to use some typeclass (Foldable would be right?) that supports different types of collections. > import Data.Foldable as F > > latestUpdate :: Foldable a => [a] -> a > latestUpdate = F.maximumBy (comparing updateTime) But yeah your point is very very valid. I've programmed some Java in my days and while i wouldn't call myself an expert i don't see any immediate "unawkward" abstractions that can be performed in the java code. If you have all Foo's implement a method "getProperty(String)" that takes a String and returns the property of that class instance you _could_ implement the static method (correct java-lingo?) maximumBy(String s) that is roughly like this: class MyClass { public static Foo maximumBy (Iterator <Foo> iterator, String s) { long max = 0; Foo maxFoo = Null; while (iterator.hasNext()) { item = iterator.getNext(); if (item.getProperty(s).compareTo(max)) { max = item.updateTime; maxFoo = item; } } return maxFoo; } } and then Foo latestUpdate (Iterator <Foo> i) { return MyClass.maximumBy(i,"latestUpdate"); } Totally untested code ofcourse... The awkward part obviously comes from the fact that properties aren't functions that you can pass to the maximumBy-method. Anyhow, Haskell's nice Polymorphism is surely a good argument for it (but not for functional programming per se right?) Mattias _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe