A Stroll with Groovy (part I)

Back in March I had an opportunity to attend the No Fluff, Just Stuff Symposium (NFJS) here in St. Louis. A variety of the classes focused on the dynamic languages growing up around the Java Virtual Machine bytecode, namely JRuby, Jython, and Groovy. I've always loved dynamic languages, and these Java-based languages already had my attention (but alas not my time), but the focus at NFJS made me determined to try them out. It's took a while since NFJS for me to break free some time, but I finally had a chance to take Groovy for a spin.

Groovy is an object-oriented dynamic language that feels a lot like Java with Ruby-isms. You can script in Groovy and run them using the Groovy interpreter. Since Groovy is Java bytecode-based, your Groovy scripts and classes can access any Java libraries. Therefore you can script Java with Groovy. Likewise, you can also use the Groovy compiler to generate bytecode versions of your Groovy classes and use them from Java. Similar things can be done with JRuby and Jython, but Groovy's syntax, Ruby-isms aside, is generally Java syntax without a lot of the syntactic cruft like semi-colons. This smoothes the way for the busy Java developer who may not have the time to absorb a whole new syntax like Python or even Ruby. Even a C# developer would find it a fairly smooth transition since Java and C# have so many fundamental similarities. Having said that, I do believe in the Pragmatic Programmer philosophy about the value of learning new and different languages.

For my first stroll with Groovy, I decided to write a script that approximates the value of PI using the Monte Carlo method. To approximate PI using the Monte Carlo method, we generate a set of random coordinate (x/y) points. We run each point through a calculation to determine if that point falls within the "circle." If it does, then we count that point, if it does not, we throw it away. In the end we have two values: the number of points we generated, and the number of those points that fell into the circle. We pump those two values through a final calculation, and we have an approximated value for PI.

(Caveat emptor! I'm just learning Groovy, so I'm sure I'm missing Groovy-isms and other best practices!)

Here's my first script:

package com.appistry.demo.groovy 
 
totalPoints = 500000 
generator = new Random(System.currentTimeMillis()) 
pointsInCircle = 0 
 
totalPoints.times { 
  double x = generator.nextDouble() 
  double y = generator.nextDouble() 
  if (Math.sqrt(x * x + y * y) <= 1) 
    pointsInCircle++ 
} 
 
pi = 4.0 * pointsInCircle / totalPoints 
 
println "\nApproximating PI using Monte Carlo Method" 
println "Points Attempted : " + totalPoints 
println "Points in Circle : " + pointsInCircle 
println "  Approximate PI : " + pi

So what do we have here?

  • Groovy supports packaging of course, since you can interchange Groovy objects and Java objects.
  • We've dropped the semicolons. They are legal, so a stray one won't hurt us, but we don't need them.
  • We aren't forced to declare our datatypes, as you see with totalPoints, pointsInCircle, etc. However, we can declare them explicitly if we wish. In the case of x and y, our randomly generated point coordinates, I've stated they are double types. There is also a "def" keyword used in defining attributes, but I'll save that for later.
  • The Random library being used here is the standard Java Random library. The same goes for the Math.sqrt call. That's regular Java libraries being leveraged by Groovy.
  • Note the non-Java like loop construct with totalPoints.times. Groovy has closures, and you can define your own. The resulting syntax is very Ruby-like and quite nice.
  • The higher the value of totalPoints you set, the longer the script will take to execute, but also the "better" the value of PI you'll approximate.

To run this, you'll need to download and install Groovy from http://groovy.codehaus.org/. One gotcha for Windows users, when I last installed, Groovy did not like being under "Program Files," and so I installed it to "C:\groovy." This may have been changed by now.

After Groovy is installed, save the script above into a file and name it compute_pi.groovy. Run the script like so:

$ groovy compute_pi.groovy
 
Approximating PI using Monte Carlo Method
Points Attempted : 500000
Points in Circle : 392895
  Approximate PI : 3.14316

So that's my first script in Groovy.

This example doesn't get into defining classes in Groovy. It's as clean and simple as you'd hope.

In my next blog post we'll define a class and use it from the script.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
5 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.