❯ Guillaume Laforge

Groovy: a sample script

In the IntelliJ forums, I came across an off-topic (but funny) post by Robert Gibson who was wondering :

Somebody told me once that there are only two words in the English language which contain each vowel, once only, in alphabetical order. Anybody know what the other one is?

Indeed, there are more than two words corresponding to those constraints. I then wrote a little Java class which took all the words of a words file (with 100k words) and tested if they matched a regexp corresponding to those constraints.

But for the fun and for living on the cutting edge, I decided to also write a Groovy script doing the very same thing. In fact, it’s much more consise, and overall, I’ve fallen in love with closures. I’d love to have closures in Java.

Here is my little script (in fact, it is my second ever written Groovy script after my LOAF implementation)

def foo = new File("wordlist.txt")
foo.eachLine{ word ->
  if (word ==~ "^[^aeiou]*a[^aeiou]*e[^aeiou]*i[^aeiou]*o[^aeiou]*u[^aeiou]*$" ) 
    println word
}