-
-
Save ilmoralito/73e43f4f09a5c91ce0d708d4f31fc703 to your computer and use it in GitHub Desktop.
Groovy List Destructuring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def (a,b,rest) = [0, 1, 2..-1].collect { [1,2,3,4][it] } | |
assert a == 1 | |
assert b == 2 | |
assert rest == [3,4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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