Loopy with Groovy
There’s been a lot of people asking for a Java loop syntax in groovy. A lot if not almost all usages however are cleaner without it, once you know some idiomatic groovy.
Here are just some of the examples of looping constructs available in groovy:
def items = [‘one’, ‘two’, ‘three’, ‘four’]
results = []
items.eachWithIndex { item, idx ->
results << idx
}
assert results == [0,1,2,3]
results = []
items.eachWithIndex { item, idx ->
results << idx
}
assert results == [0,1,2,3]
There are a whole range of Groovy features at work here, from inline lists to ranges, GDK methods added to JDK classes such as step() on numeric types and of course closures.