# [Newbies] Re: Another extension proposal -> subsets

Zulq Alam me at zulq.net
Wed Jul 23 16:47:26 UTC 2008

```Couldn't find anything obvious but remembered #permutationsDo: which may
help. Not sure if these does what you want, but maybe it can be adapted?

Set>>subsets
| subsets |
subsets := Set with: self.
self asArray permutationsDo: [:e | subsets add: e asSet].
self do: [:e | subsets addAll: (self copyWithout: e) subsets].
^ subsets

#(1 2) asSet subsets.
"a Set(a Set(1 2) a Set() a Set(2) a Set(1))"

#(1 2 3) asSet subsets.
"a Set(a Set(1 2) a Set() a Set(2) a Set(1 2 3) a Set(2 3) a Set(3) a
Set(1 3) a Set(1))"

Z.

cdrick wrote:
> When we don't find method, we reinvent the wheel. Ok, so this is what
> happened with my quest on rounding float. So here is another example.
> I needed a method subsets ... to have all subsets of a Set...
>
> #(1 2 ) asSet subsets " -> a Set(a Set(1 2) a Set(1) a Set(2) a Set()) "
>
> Is there already something ? I couldn't find so here is what I did ?
> probably hackish (because of the binary mask) so feel free to comment
> ;)
>
> Set>>subsets
> | subsetsSize subsets workArray |
> 	workArray := self asArray.
>  	subsetsSize := 2 raisedTo: self size.
> 	subsets := OrderedCollection new.
> 	1 to: subsetsSize do: [:ea |
> 		subsets add: ((workArray masquedBy: (ea printStringBase: 2))
> asSet)]. "masque par une conversion binaire"
> 	^subsets asSet  "could be an array of sets"
>
> ArrayedCollection>>masquedBy: aBitString
> | result entry bitString |
> 	entry := self reverse.
> 	bitString := aBitString reverse.
> 	result := OrderedCollection new.
> 	1 to: (self size) do: [:ea |
> 		 ((bitString  at: ea ifAbsent: []) = \$1) ifTrue: [result add: (entry
> at: ea)]].
> 	^result reverse
>
> Cédrick
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

```