Closure ( { -> } )
A block of code that can be assigned to a variable.
Syntax
def f = { param -> expression }Description
A closure is a small function that can be assigned to a variable. A single parameter can be accessed via its default name (it). It is widely used in methods like each/collect.
Example
Loading editor…
Expected output
36 1 2 3
When to use?
each/collect callbacks, short functions.