Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All subsets of a list in Perl 6

Tags:

math

set

raku

I wonder (remembering that Perl 6 has everything you could wish), whether there are some built-in instruments that can help to produce all the non-empty subsets (order doesn't matter) of a list.

E.g., I have a list:

my @a = 1, 2, 3;

I need a function f so that f(@a) will produce:

((1), (2), (3), (1, 2), (1, 3), (2, 3), (1, 2, 3))
like image 591
Eugene Barsky Avatar asked May 09 '18 19:05

Eugene Barsky


1 Answers

@a.combinations(1..*)

will return the Seq you're looking for. Note that without the argument, an empty list would be generated as first element.

like image 169
Christoph Avatar answered Nov 15 '22 21:11

Christoph