Working with Groovy Closures
A Groovy closure is a code block or a method pointer, when we use a closure it gives us a default variable called “it” and it is the one that receives the data that is sent to it.
Closure definition:
def clos = { println it }
clos("Hello")
Output:
Hello
Of course, we can also make our own closures with the parameters that we decide. The syntax would be the following:
def nameClosure = { parameter1, parameter2 ->
// Operations to perform with the parameters
}
To finish, I will give you a simple exercise:
Given a written composition, make a Groovy script that does the following:
- Take only the first letter of each word in that wording, and convert it to uppercase.
- Respect punctuation marks and line breaks.
Sample input text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aliquam vel luctus orci
Groovy Code
String text = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aliquam vel luctus orci'''
// Definition of the closure
def firstLetterToUpperCase = { it[0].toUpperCase() + it.substring(1) }
String result = text.split(' ').collect(firstLetterToUpperCase).join(' ')
println result
Output:
Lorem Ipsum Dolor Sit Amet, Consectetur Adipiscing Elit.
Aliquam Vel Luctus Orci