Introduction to Groovy (Part 3)
Wednesday, February 14th, 2007Closures in Groovy
The concept of a closure in computer programming has been around for a long time, and is arguably a very powerful construct in your applications. While the basics of closures are not hard to understand, I will leave the theory out of this blog post and direct you to the Wikipedia entry for closures for a more detailed description.
Consider this following Java code:
[java]class MyClass {
public int add(int x, int y) {
return x + y;
}
}[/java]
The code above defines a method named add that takes two parameters, adds them together, and returns them. It is a block of code that is named. With closures, you have a block of code that is considered to be anonymous that you can use throughout your application. In other words, it isn’t formally named like the add method above, but it is an anonymous code block that can be assigned to a variable.
The equivalent closure in Groovy would look like this:
[groovy]def add = {x, y -> x + y}[/groovy]
The definition of a closure block looks like this:
[code]{ [parameterList ->] code}[/code]
The parameterList is a comma seperated list of parameters that you will pass into the closure, and it is entirely optional. If you do not define a parameter list, the closure will have one implicit parameter named it that you can use in the closure (and the value of it will be null if you do not pass anything in to the closure).
Also remember than in Groovy you don’t have to specify a return statement. It will automatically return the last evaluated value in the block. This makes for much shorter and easier to read code.
By this time you are probably wondering what the utility of this is, and how it is different than a method or using inner classes in Java. Think about this: With closures, you can assign it to a variable name and pass it around like you would a nomral variable. You are probably thinking, “So?” but this actually introduces a lot of power and flexibility into your programming arsenal.
Let’s take a look at a real Groovy example of the utility of closures. Lists in Groovy have a method called findAll which returns a sublist based on your search criteria. You can pass this a closure to determine your search criteria:
[groovy]["Tim", "Timmy", "John", "Johnny", "Bill"].findAll { it.contains(”Tim”) }[/groovy]
The above example defines an anonymous list and invokes the findAll method. It passes a closure into the findAll method which contains some code. Remember that if you don’t define a parameter list in a closure you can use the it variable, and that is what this is. In this case, it is a String and the findAll method expects you to define your conditions using it.
In addition to findAll, there is an each method on Lists and Maps that executes the closure you pass to it on each element in the collection.
One of the neater aspects of closures is when you use it on primatives. Primatives are autoboxed to their object counterparts in Groovy, so you can do code like this:
[groovy]10.times { println “Hello, world!” }[/groovy]
The Long class has a method called times() which takes a closure as an argument. Whatever the value of the number, it will execute the closure code that many times. Pretty neat, isn’t it?
For more detailed information on closures in Groovy, check out the Groovy documentation on closures. This contains a very in-depth look at closures.
I hope this Introduction to Groovy series has sparked some interest in the language. Groovy really is a fun language to work with and I would encourage any programmer to check it out.
- Introduction to Groovy (Part 1)
- Introduction to Groovy (Part 2)
- Introduction to Groovy (Part 3)