Tim Bray's Wide Finder Project - Groovy 
2007-10-21, 14:28
Tim Bray's Wide Finder Project has been drawing implementations in a number of language, but none yet in Groovy. Time to put that right …

def hits = [:]
def pat = ~"GET /ongoing/When/\\d{3}x/(\\d{4}/\\d{2}/\\d{2}/[^ .]+) "

new File( args[ 0 ] ).eachLine{ line ->  
  def matcher = line =~ pat
  if( matcher.count )  {
    def key = matcher[ 0 ][ 1 ]
    def val = hits[ key ]
    hits[ key ] = ( val == null ) ? 1 : ++val  
  }
}

hits.entrySet().sort{ a, b -> 
  b.value.compareTo( a.value )
}[ 0 .. Math.min( 9, hits.size() - 1 ) ].each{ entry ->
  println "$entry.value: $entry.key" 
}

Looking at this what's striking is that although Groovy can be written pretty much like full-on prolix Java, it's also possible to write cryptic and unmaintainable compact and elegant code like this with it too.

For benchmarking I stuck together 10,000 copies of Tim's test data to get a log file of approx 1.3 GB. On my humble X31 The original Ruby script gets through this in 92 seconds; Groovy takes 115. Not too shabby. Maybe this is a good excuse to explore how multi-threading would look in Groovy …

I have enjoyed using Ruby and Python in their time, but now that Groovy is here I don't think I'll ever be using them again. It's not so much the terseness which is attractive, but the proper Java underpinning with all those excellent libraries (not least for XML processing) on tap.
4 comments ( 3473 views )  | permalink  |  stumble this |  digg it!


<<First <Back | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | Next> Last>>