Groovy closures are powerful. A closure can be passed to methods as argument or defined as a variable. We can even return closures from methods or other closures. We can use the returned closure to execute the logic from the closure with the explicit call() method or the implicit syntax with just the closure object followed by opening and closing parentheses (()).

// Method returns a closure. Method could
// also have been another closure to return
// the closure.
def repeater(times) {
    { value -> value * times }
}

// Use explicit call() method on the return closure
// object from the repeater() method.
assert repeater(2).call('mrhaki') == 'mrhakimrhaki'

// Use implicit call() method on the return closure
// object from the repeater() method. This
// might looks strange at first...
assert repeater(2)('mrhaki') == 'mrhakimrhaki'

We can even use a closure parameter and use it in the return closure code. We rewrite the previous example and this time use a closure instead of method to define repeater:

// Extra transformer argument with default
// closure implementation.
def repeater = { times, transformer = { it } ->
    { value -> transformer(value) * times }
}

assert repeater(2).call('mrhaki') == 'mrhakimrhaki'
assert repeater(2)('mrhaki') == 'mrhakimrhaki'

assert repeater(2) { it.toUpperCase() } ('mrhaki') == 'MRHAKIMRHAKI'
assert repeater(2, { it.reverse() })('mrhaki') == 'ikahrmikahrm'

Notice we define a transformer parameter that has a default closure that simply returns the argument of the closure, so the old assertions still work. At lines 9 and 10 we pass a closure as argument to the repeater closure using two different syntaxes.

Code written with Groovy 2.0.4

Original blog post

shadow-left