Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List Subsets in Groovy (inspired from Java 8 in Action#13.2.4)
def list = [1,4,9]
println subsets(list)
List<List<Integer>> subsets(List<Integer> list) {
if(!list) {
return [[]]
}
final List<List> directSubs = list.tail().collect {
return [list.head(), it]
} + [list.tail()]
return ([[list.head()]] + directSubs + subsets(list.tail())).unique()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.