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@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 add: (entry
at: ea)]].
       ^result reverse

Cédrick

_______________________________________________
Beginners mailing list
Beginners@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners