USING: arrays combinators kernel make math math.combinatorics math.ranges sequences sequences.deep ; IN: combinatorics : all-subsets ( seq -- subsets ) dup length [0,b] [ [ dupd all-combinations [ , ] each ] each ] { } make nip ; : (selections) ( seq n -- selections ) dupd [ dup 1 > ] [ swap pick cartesian-product [ [ [ dup length 1 > [ flatten ] when , ] each ] each ] { } make swap 1 - ] while drop nip ; : selections ( seq n -- selections ) { { 0 [ drop { } ] } { 1 [ 1array ] } [ (selections) ] } case ;