Ben Coman wrote
> On 31 December 2017 at 04:00, Andy Burnett <

> andy.burnett@

> >
> wrote:
> 
>> I suspect I am missing something very obvious, in which case could some
>> kind soul put me straight?
>>
>> I have the following code
>>
>> col := OrderedCollection new.
>> (1 to: 9) permutationsDo: [ :each | col add: (each )].
>>
>> I thought this would give me a collection containing all the
>> permutations.
>> However, what actually happens is that each of the arrays has been
>> 'magically' sorted back into numerical order.  If I change the code to
>> read
>>
>> col := OrderedCollection new.
>> (1 to: 9) permutationsDo: [ :each | col add: (each asOrderedCollection
>> )].
>>
>> Then the permutations are retained.  Is this how it is supposed to work?
>> Or, am I doing something wrong?
>>
>> Cheers
>> Andy
>>
> 
> 
> Its not so much that your first example sorted the permutations, but that
> the collection contained only one permutation.
> I've swapped the order of your examples and downsized them to the simplest
> case to observe.
> Something seems broken.  It works as expected if the "copy" is
> uncommented.

It's not broken, but certainly deserves a comment as to why it operates
inline; 
In the general case, one should not use/care about any given permutation
outside the block.
Allocating n! instances of an n-sized array by default, is a bad tradeoff
considering performance degradation for uses where n >> 3.
In the first case, a user can add a simple #copy, while in the second case,
a user would probably want/have to reimplement the method entirely.

Cheers,
Henry



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply via email to