Loading...

December 30, 2009

Groovy Goodness: Putting Our List in Reverse

Groovy has so many extra methods for lists to make life easier. For example we can reverse a list with the reverse() method. But we can also loop reverse through a list with reverseEach().

def list = [10, 20, 30]

assert [30, 20, 10] == list.reverse()

def result = []
list.reverseEach {
    result << it * 2
}

assert [60, 40, 20] == result