Monday, April 15, 2013

Groovy Tutorial Pt 1

Nothing sinks in a concept like applying it to the real word. In this blog, I'll attempt to teach even a non-CS layman Groovy by building an application from the ground up.

Setup:
See a web based version of the Groovy Console to use as your sandbox. When using the web based version don't forget to switch tabs to "Output".

Lesson 1: Building Blocks

Let's do some fun stuff. Open up your Groovy Console (or web console) and enter in the lines:
println 'I <3 coding.'
System.out.println("I <3 coding.");
You will get some output that displays two messages to the user. Next do this:
def num = 2
def three = 3
println "Lucky number " + (num + three)
If your coming from a Java background, you'll recognize the second 'I <3 coding' statement. This is straight Java syntax. Groovy is almost 100% backwards compatible with all Java syntax. This is handy information if you can't figure out how to do something in Groovy, just do it in Java! The first statement 'prints a line of text to standard out'. println is actually a function, and your giving it a single string: "I <3 coding" as a parameter.
The second code block is more fun. As you figured out, your declaring a variable (remember algebra?) called num and giving it a value of 2. def is like saying "define this thing for me so I use it in the future". Groovy is loosely typed. You don't have to say "define an integer" or "define a string". Groovy will figure out the details for you. Line 3 will give "Lucky number 5".
Checkpoint: Write two or three println's and figure out how to break the syntax.
Next, try this:

def num = 2
def nums = [num, 3, 4]
nums.add(5)
nums.each { a -> println "Hello $a" }

Line 2 enters the realm of data structures. These are just specially designed backpacks that hold your data. Here, we define a List with the syntax of [].  This particular backpack holds one variable and two integers. In the last line of code we use a method called each to iterate through each element in the list and print it to the console. You can call each on every variable you ever define (or declare). Not all variables (or objects) will act the same though. The two braces {} define a closure, which is a method (a function) that is created and called on the fly. The it-> is saying "expect a single parameter and call it a, then run everything between the -> and }". The statements after the pointing arrow can span as many lines as they need to. But how does it know to do this to each value in the list?? The each method needs you to define a closure as a single parameter because it plans to grab each element in order and, in turn, pass each element to that closure you've defined. In Java you would write something like:
for (int i = 0; i < nums.size(); i++) {
  System.out.println("Hello " + nums.get(i));
}
Finally we have a new string defined as "Hello $a". This is common scripting syntax where you can tell Groovy to replace $a in the string with the value of a. In Groovy, there is always multiple ways to accomplish the same thing. For instance, you can add elements to a list by doing a "right shift": nums << 5.
Checkpoint: Swap out the double quotes with single quotes to see a key syntax difference. 
Add elements to your list.
Try the same each closure on a string (ie "a string".each { ... })

If you have specific examples in mind of something you want to do, the Cookbook Examples page is a great resource. Next time we'll make sure you install Groovy on your machine and we'll write a small console application that you can run on any machine with Java installed.
Groovy on, young and faithful warrior.

No comments:

Post a Comment