Skip to content

Instantly share code, notes, and snippets.

@geoand
Created February 12, 2016 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoand/97b40fc0170df387cb03 to your computer and use it in GitHub Desktop.
Save geoand/97b40fc0170df387cb03 to your computer and use it in GitHub Desktop.
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