Skip to content

Instantly share code, notes, and snippets.

@kdabir
Created September 27, 2014 17:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kdabir/c2da0a460d321713f3da to your computer and use it in GitHub Desktop.
Save kdabir/c2da0a460d321713f3da to your computer and use it in GitHub Desktop.
Groovy List Destructuring
def (a,b,rest) = [0, 1, 2..-1].collect { [1,2,3,4][it] }
assert a == 1
assert b == 2
assert rest == [3,4]
List.metaClass.destructure = { ...n->
n.collect { delegate[it]}
}
def (a,b,rest) = [1,2,3,4].destructure(0, 1, 2..-1)
assert a == 1
assert b == 2
assert rest == [3,4]
def destructure(list,...n) {
n.collect {list[it]}
}
def (a,b,rest) = destructure([1,2,3,4],0, 1, 2..-1)
assert a == 1
assert b == 2
assert rest == [3,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment