Hi! I'm Nicolas and I'm interested in information visualization, JavaScript and web standards.
I currently work as a Data Visualization Scientist at Twitter. I wrote PhiloGL, the JavaScript InfoVis Toolkit and V8-GL.
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?
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.
Features I like about the language
Some of the features I used and liked about Groovy:
List and Hash literals
As opposed to Java, Groovy provides List and Hash literals:
//Create an empty ListemptyList=[]//Create an empty HashemptyHash=[:]//Create a ListsomePeople=["John","Jack","Sarah"]//Create a Hashext=[Ruby:'rb',Python:'py',C:'c',Groovy:'groovy']
List and Hash traversing and manipulation
In terms of List and Hash manipulation, Groovy offers the same expressiveness as Python and Smalltalk:
//Create a ListsomePeople=["John","Jack","Sarah"]//Copy first two List elementscopy=somePeople[0..1]//["John", "Jack"]//Grab last list elementlastElem=somePeople[-1]//"Sarah"//Closures are first class values in Groovy, their syntax is {}.//For unary lambdas an implicit 'it' variable is created//Any element starting with capital letters?somePeople.any{it[0]in'A'..'Z'}//true//We can also use regex a la PerlsomePeople.any{it=~/[A-Z].*/}//true//Print names with new linessomePeople.each{printlnit}//Create a Hashext=[Ruby:'rb',Python:'py',C:'c',Groovy:'groovy']//print an elementprintlnext.'Ruby'//rbprintlnext['Ruby']//rb//iterate through a hash elementsext.each{key,value->printlnkey+': '+value}//will print Ruby: rb, etc
Defining functions
Functions are defined like this:
//Define a square functiondefsquare(val){val*val}//Or assign a closure to a square variable:square={it*it}
Simple.
Java classes extensions
One 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:
//Print dir file namesnewFile("/some/dir/").eachFile{printlnit.name}//Print file text contentprintlnnewFile("/some/file.txt").getText()//Print file content with line numbersnewFile("otherfile.txt").eachLine{it,line->printlnline+": "+it}
Other libraries
At 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.