# [Newbies] Another extension proposal -> subsets

Marcin Tustin mm3 at zepler.net
Wed Jul 23 16:11:01 UTC 2008

```Googling "smalltalk powerset" turns this up:
http://www.foldr.org/~michaelw/log/2005/09/

On Wed, Jul 23, 2008 at 11:51 AM, cdrick <cdrick65 at gmail.com> 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
> at: ea)]].
>        ^result reverse
>
> Cédrick
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20080723/04fc34d6/attachment.htm
```