Created
February 12, 2016 14:43
-
-
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)
This file contains 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 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