[Squeak] Newbie permutations

Avi Bryant avi at beta4.com
Tue Apr 9 07:30:12 UTC 2002


On Tue, 9 Apr 2002, Duane Maxwell wrote:

> > | foo |
> > foo := OrderedCollection new.
> > #(1 3 5) permutationsDo: [:each | foo add: each].
> >
> > Does that do it for you?
>
> No, unfortunately this gives:
>
> an OrderedCollection ( #(1 3 5) #(1 3 5) #(1 3 5) #(1 3 5) #(1 3 5) #(1 3
> 5) )

Aha.  Since this threw me for a second, I should probably explain -
permutationsDo: is reusing the same array for each permutation.  Hence the
need for the #copy in the correct version,

#(1 3 5) permutationsDo: [:each | foo add: each copy]

Is there a widespread convention at work here, or is this just a
pecularity of the implementation of that particular method?




More information about the Squeak-dev mailing list