While at work last week I decided to make a small program in the Groovy programming language. I needed to build a small file processing program that used some Java libraries built at work, but I didn’t want to code five or six Java classes to do so. Since performance wasn’t a big concern, I decided to take a look at some JVM based languages.
There are lots of programming languages targetting the JVM, so why Groovy?
Groovy is a highly dynamic language, that takes things from Python, Ruby and Smalltalk. Since these are programming languages I used before and I’m quite comfortable with, Groovy seemed like a good match.
Also, Groovy is very easy to learn, having an almost-zero learning curve. Since I had to do this in a couple of days, I didn’t want to spend a lot of time learning a programming language’s syntax and semantics. I know how to use Python and I know how to use Ruby/Smalltalk, I just want to do the same things in the JVM.
Another very interesting thing (that doesn’t concern the language itself, but it’s quite helpful) is that Groovy, along with OCaml and Perl, has a 100% completness score at PLEAC. That means that you can find complete examples of: Strings, Numbers, Arrays, Hashes, Dates and Times, Pattern Matching, File Access, File Contents, Directories, Subroutines, References and Records, Packages, Libraries and Modules, Classes and Objects, Database Access, User Interfaces and a lot more here.
Some of the features I used and liked about Groovy:
List and Hash literalsAs opposed to Java, Groovy provides List and Hash literals:
List and Hash traversing and manipulationIn terms of List and Hash manipulation, Groovy offers the same expressiveness as Python and Smalltalk:
Defining functionsFunctions are defined like this:
Simple.
Java classes extensionsOne of the things I find really nice about Groovy, is that it extends Java SE with useful functions. You can find the extensions here.
Java File class extensions are pretty cool:
Other librariesAt work I had to deal with XML data, and found a very interesting and high level XML manipulation library called XmlSlurper:
Groovy is a versatile scripting language built on top of the JVM. It provides useful features taken from Ruby, Python and Smalltalk, with full access to all Java libraries. It also extends Java classes with useful methods and iterators. If you aren’t that worried about performance (still runs faster than Ruby, Python 3 and Perl), I’d recommend you to take a look at it. Hope this was helpful enough to get a feeling of the language.