Groovy vs. Google Collections: Round #1
In my last post, Dan Lewis (my friend and former coworker) responded with some counter-code from Google’s collections package. Instead of attempting to snap back with some witty technical retort, I challenged Dan to a code-off. Groovy collections vs. Google collections (in Java). Here is round one, and I’m going first with an example of finding combinations of collections. Hopefully, you can soon find Dan’s response in Google collection code-style on his blog.
def boys = ['Paco', 'Sven', 'Roger', 'Emelio'] def girls = ['Julia', 'Prudence', 'Lucy']
Now we have two simple lists, one of boys and one of girls. So what if these are all dancers, and we’d like to know all the combinations of dancers there could be? With groovy, this is extremely easy.
def combos = [boys, girls].combinations()
Could it be any easier? This creates a list of lists, each inner list containing a possible combination:
Now it would be nice if i could split the list up in some way, maybe keying by male or female. Groovy’s groupBy collection method is an easy way to do this. In the closure you provide, you simple return the key that you want to group the collection by, and it will return a map with that key pointing to a list of values. In the following example, because I know the first element of each combination is the boy and the second is the girl, I just specify a key by element position.
def groupedByBoys = combos.groupBy { it[0] }
def groupedByGirls = combos.groupBy { it[1] }
Which returns these two groupings:
Beat that, Google Collections!
April 5th, 2008 at 8:41 am
I have posted a response.
Matthew Taylor Says:April 5th, 2008 at 9:46 am
In all fairness, the complete groovy code to print out the same as Dan’s is here, and it is 12 lines:
def boys = ['Paco', 'Sven', 'Roger', 'Emelio'] def girls = ['Julia', 'Prudence', 'Lucy'] def combos = [boys, girls].combinations() println combos def groupedByBoys = combos.groupBy { it[0] } def groupedByGirls = combos.groupBy { it[1] } groupedByBoys.each{ key, val -> println "Partners of ${key}: ${val.collect{ it[1] }}" } groupedByGirls.each{ key, val -> println "Partners of ${key}: ${val.collect{ it[0] }}" }This prints out the following:
[[”Paco”, “Julia”], [”Sven”, “Julia”], [”Roger”, “Julia”], [”Emelio”, “Julia”], [”Paco”, “Prudence”], [”Sven”, “Prudence”], [”Roger”, “Prudence”], [”Emelio”, “Prudence”], [”Paco”, “Lucy”], [”Sven”, “Lucy”], [”Roger”, “Lucy”], [”Emelio”, “Lucy”]]
Partners of Emelio: [”Julia”, “Prudence”, “Lucy”]
Partners of Roger: [”Julia”, “Prudence”, “Lucy”]
Partners of Sven: [”Julia”, “Prudence”, “Lucy”]
Partners of Paco: [”Julia”, “Prudence”, “Lucy”]
Partners of Lucy: [”Paco”, “Sven”, “Roger”, “Emelio”]
Partners of Prudence: [”Paco”, “Sven”, “Roger”, “Emelio”]
Partners of Julia: [”Paco”, “Sven”, “Roger”, “Emelio”]
April 5th, 2008 at 10:18 am
This is awesome. I’m looking forward to round 2!
dangertree techblog » Blog Archive » Groovy vs. Google Collections: Round #2 Says:April 8th, 2008 at 10:25 pm
[…] dangertree techblog sweaty programming and disconnected gibberish « Groovy vs. Google Collections: Round #1 […]
Grails Podcast Episode 53 « Sven Haiges’ Personal Blog Says:April 14th, 2008 at 12:46 pm
[…] vs Google Collections. Matt on Groovy. Dan on Google […]
Matthew Taylor Says:April 19th, 2008 at 7:25 am
One more neat thing about combinations that I didn’t mention: You aren’t restricted to two lists to combine. It can be as many as you want:
def girls = [’Julia’, ‘Prudence’, ‘Lucy’]
def locs = [’Milan’, ‘Prague’, ‘St. Louis’, ‘Toronto’, ‘Hong Kong’, ‘Los Angelos’]
def combos = [boys, girls, locs].combinations()
produces:
[[”Paco”, “Julia”, “Milan”], [”Sven”, “Julia”, “Milan”], [”Roger”, “Julia”, “Milan”], [”Emelio”, “Julia”, “Milan”], [”Paco”, “Prudence”, “Milan”], [”Sven”, “Prudence”, “Milan”], [”Roger”, “Prudence”, “Milan”], [”Emelio”, “Prudence”, “Milan”], [”Paco”, “Lucy”, “Milan”], [”Sven”, “Lucy”, “Milan”], [”Roger”, “Lucy”, “Milan”], [”Emelio”, “Lucy”, “Milan”], [”Paco”, “Julia”, “Prague”], [”Sven”, “Julia”, “Prague”], [”Roger”, “Julia”, “Prague”], [”Emelio”, “Julia”, “Prague”], [”Paco”, “Prudence”, “Prague”], [”Sven”, “Prudence”, “Prague”], [”Roger”, “Prudence”, “Prague”], [”Emelio”, “Prudence”, “Prague”], [”Paco”, “Lucy”, “Prague”], [”Sven”, “Lucy”, “Prague”], [”Roger”, “Lucy”, “Prague”], [”Emelio”, “Lucy”, “Prague”], [”Paco”, “Julia”, “St. Louis”], [”Sven”, “Julia”, “St. Louis”], [”Roger”, “Julia”, “St. Louis”], [”Emelio”, “Julia”, “St. Louis”], [”Paco”, “Prudence”, “St. Louis”], [”Sven”, “Prudence”, “St. Louis”], [”Roger”, “Prudence”, “St. Louis”], [”Emelio”, “Prudence”, “St. Louis”], [”Paco”, “Lucy”, “St. Louis”], [”Sven”, “Lucy”, “St. Louis”], [”Roger”, “Lucy”, “St. Louis”], [”Emelio”, “Lucy”, “St. Louis”], [”Paco”, “Julia”, “Toronto”], [”Sven”, “Julia”, “Toronto”], [”Roger”, “Julia”, “Toronto”], [”Emelio”, “Julia”, “Toronto”], [”Paco”, “Prudence”, “Toronto”], [”Sven”, “Prudence”, “Toronto”], [”Roger”, “Prudence”, “Toronto”], [”Emelio”, “Prudence”, “Toronto”], [”Paco”, “Lucy”, “Toronto”], [”Sven”, “Lucy”, “Toronto”], [”Roger”, “Lucy”, “Toronto”], [”Emelio”, “Lucy”, “Toronto”], [”Paco”, “Julia”, “Hong Kong”], [”Sven”, “Julia”, “Hong Kong”], [”Roger”, “Julia”, “Hong Kong”], [”Emelio”, “Julia”, “Hong Kong”], [”Paco”, “Prudence”, “Hong Kong”], [”Sven”, “Prudence”, “Hong Kong”], [”Roger”, “Prudence”, “Hong Kong”], [”Emelio”, “Prudence”, “Hong Kong”], [”Paco”, “Lucy”, “Hong Kong”], [”Sven”, “Lucy”, “Hong Kong”], [”Roger”, “Lucy”, “Hong Kong”], [”Emelio”, “Lucy”, “Hong Kong”], [”Paco”, “Julia”, “Los Angelos”], [”Sven”, “Julia”, “Los Angelos”], [”Roger”, “Julia”, “Los Angelos”], [”Emelio”, “Julia”, “Los Angelos”], [”Paco”, “Prudence”, “Los Angelos”], [”Sven”, “Prudence”, “Los Angelos”], [”Roger”, “Prudence”, “Los Angelos”], [”Emelio”, “Prudence”, “Los Angelos”], [”Paco”, “Lucy”, “Los Angelos”], [”Sven”, “Lucy”, “Los Angelos”], [”Roger”, “Lucy”, “Los Angelos”], [”Emelio”, “Lucy”, “Los Angelos”]]
May 6th, 2008 at 9:43 am
It’s Beatifull!