DevOps / Apache Groovy Interview questions
What is a closure in Groovy?
A closure is an anonymous, reusable block of code that can take parameters, return a value, and be assigned to a variable or passed around like any other object - Groovy's version of a first-class function.
Unlike a plain method, a closure captures variables from its surrounding scope at the point it's defined, so it can reference and even modify local variables from the context where it was created, even after that context has technically finished executing.
Closures are used pervasively throughout the GDK - most collection methods like each, collect, and find take a closure as their argument to define what happens per element.
More Related questions...