Search

Dark theme | Light theme

March 8, 2010

Groovy Goodness: Use Ranges as Subscript Operators

In Groovy we can use variables as subscript operators to get values from a list.

def list = ['Java', 'Groovy', 'Scala']

def index = 0
def indices = [0,2]
def range = (1..-1)

assert 'Java' == list[index]
assert ['Java', 'Scala'] == list[indices]
assert ['Groovy', 'Scala'] == list[range]