CHAPTER 9 CLOSURES

Groovy closures are a powerful way of representing blocks of executable code. Since closures are objects, they can be passed around as method parameters, for example. Because closures are code blocks, they can also be executed when required. Like methods, closures can be defined in terms of one or more parameters. A significant characteristic of closures is that they can access state information. This means that any variables in scope when the closure is defined can be used and modified by the closure.

One of the most common uses for a closure is processing a collection. For example, we can iterate across the elements of a collection and apply the closure to them. Groovy’s closures are one feature that make developing scripts ...

Get Groovy Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.