2

Groovy allows unfolding lists in assignment, as in:

(x, y) = [1, 2]

So I assumed something similar would work in a for loop, as in:

list = [[1, 2], [2, 4], [3, 6]]
for ((elm1, elm2) in list) {...}

Which turns out to be a syntax error. Is this style not possible or is there some trick to it I'm missing?

1 Answer 1

3

I guess it won't work with the for loop (or I definitely don't know the syntax), however a two-argument closure can be used to iterate such a List, unfold the tuples:

def list = [[1, 2], [2, 4], [3, 6]]
assert list.collect { a, b -> a + b } == [3, 6, 9, ]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.