Monday, April 22, 2013

Groovy Tutorial Pt 1.9

Installing Groovy

Java Prereq

To do real applications you'll need to install Groovy and Java on your machine. Go here to get Java 1.7 SE (Standard Edition) SDK (Software Developers Kit) and download an appropriate version. I am running Windows 7 so I'll use the highlighted one. If you don't know which one to get, ask Google. Just so you know, the JDK refers to Java Developers Kit, and GDK is Groovy Developers Kit. It's not rocket science.
Follow the installer to do all standard settings. It will install a few different tools, most of which you won't use, hence, we won't talk about them.

Get Your Groove On

Next, go here and download the latest binary of Groovy. For this, I'll be using 2.1.3.

You can also use the installer, but I'll be dealing with the binary for generalized instructions. Once you've downloaded the binary you're going to unzip the contents into a file location. Mine is going to go into "C:\Program Files\Java". Right Click on the zip in your downloads folder and extract to a directory.
If you have any trouble, just use the "extract to here" menu option and copy the files to your directory of choice. Here's my groovy install:


Next, you'll need to set your environment variables to find the location of the Groovy install. Edit the system environment variables and add GROOVY_HOME and JAVA_HOME to point to your SDK's. Follow the snapshots below.
If you pull up the start menu you can search for environment settings, otherwise go to the Control Panel and look for menus that will let you to editing these properties.

You'll have to add these one by one, and remember the "Variable value" is the path to your specific installation. If you extracted the zip file into the downloads folder, you'll have to point there. Lastly, you should have a "Path" system variable already defined. Edit that variable and add on "%groovy_home%%\bin;%java_home%\bin". Every variable is denoted by % signs and are separated with a semicolon. The \bin folder is where all your executable files (called binaries) are, that why you have to add it to the Path variable.

Now, open up your command line (cmd.exe) and check your installation details. Your commands should show the same kind of output that mine does. If it does not show the same output, something went wrong and you should ask Google or give me a comment below and I'll try to help.


For the grand finale, enter "groovyConsole" into the command line.


This little sucker is going to be your best friend. From here on out we'll be using this to write pilot code and develop an app. Later on we'll probably move to full scale IDE so that you can send your app to all your friends.

Checkpoint: Go ahead and run a few commands from the previous tutorial to make the Groovy Console an acquaintance.

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.

Mission

To encourage and teach the layman to code like a boss.