One of the biggest advantages of Groovy over Java are those nice little collection helpers. For people who first encountered them in functional languages the names are confusing. Here’s a short comparison:

Functional Language Groovy
map (list, function) list.collect (closure) Applies the function to each element of the list and returns the results as a new list
filter (list, function) list.findAll (closure) Returns a list of all elements in the input list for which the function returns true
fold (list, initial element, function) list.inject (initial element, closure) Applies the function to the initial element and the first element of the list. Then applies the function to this result and the second element of the list. Get the idea?

Thanks to the author of this article at the great blog “Inside the Machine” for revealing the secret of “inject” to me!

1 Comment »

  • closure being used here makes groovy so special ,although same can have been achieved in java but would be very verbose

    Comment by ratn — August 30, 2010 @ 7:48 am

Email this Share this on Facebook Share this on LinkedIn Tweet This! RSS feed for comments on this post. TrackBack URL

Leave a comment