Closures in Groovy
Groovy supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.
In this Groovy example, we define a function intSeq()
that returns a closure. The closure closes over the variable i
to form a closure.
The main()
function demonstrates how to use this closure:
We call
intSeq()
, assigning the result (a closure) tonextInt
. This closure captures its owni
value, which will be updated each time we callnextInt
.We then call
nextInt()
multiple times to see the effect of the closure. Each call increments and returns the capturedi
value.To confirm that the state is unique to that particular closure, we create a new closure
newInts
and call it once.
To run this Groovy script, save it to a file (e.g., closures.groovy
) and execute it using the Groovy command:
This output demonstrates that the first closure (nextInt
) maintains its own state, incrementing from 1 to 3, while the second closure (newInts
) starts fresh at 1.
Closures are a powerful feature in Groovy, allowing you to create functions with persistent local state. They are commonly used in various scenarios, including callbacks, iterators, and event handlers.