[Q] SequenceableCollection>>combinations: kk atATimeDo: aBlock

Avi Bryant avi at beta4.com
Tue Dec 17 20:10:17 UTC 2002


On Tue, 17 Dec 2002, Ken Collins wrote:

> ((1 to: 12) combinations: 3 atATimeDo: [:each | Transcript cr; show: each
> printString])
>
> Shows 220 Arrays with 3 parts each. I want to put those Arrays into a new
> OrderedCollection.
>
> |oc|
> oc_OrderedCollection new.
> ((1 to: 12) combinations: 3 atATimeDo: [:e | oc add:e]).
> oc inspect
>
> Also shows 220 Arrays with 3 parts each, but they are all #(12 12 12)
> instead of what shows up in the Transcript.

|oc|
oc _ OrderedCollection new.
((1 to: 12) combinations: 3 atATimeDo: [:e | oc add: e copy]).
oc inspect

I think this came up once before - #combinations:atATimeDo: is reusing the
same array for each iteration, so you need to explicitly copy it if you
want to hang on to it.

Avi




More information about the Squeak-dev mailing list