Groovy 3 adds the
average method to collections to calculate the average of the items in the collections. When the items are numbers simply the average is calculated. But we can also use a closure as argument to transform an item into a number value and then the average on that number value is calculated.
In the following example code we use the average method on a list of numbers and strings. And we use a closure to first transform an element before calculating the average: