[squeak-dev] [BUG] Shuffling an interval raises an error

Thiede, Christoph Christoph.Thiede at student.hpi.uni-potsdam.de
Wed Feb 12 21:27:42 UTC 2020


Hi Eliot,


how do you propose to implement #shuffledBy: with #asMutable?


[self asMutable shuffleBy: aRandom] would not be guaranteed to preserve the order of the receiver.

[self asMutable copy shuffleBy: aRandom] would cause one or two copies.


Best,

Christoph

________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Eliot Miranda <eliot.miranda at gmail.com>
Gesendet: Mittwoch, 12. Februar 2020 19:05:37
An: The general-purpose Squeak developers list
Betreff: Re: [squeak-dev] [BUG] Shuffling an interval raises an error

Hi Christoph,

On Feb 12, 2020, at 9:45 AM, Thiede, Christoph <Christoph.Thiede at student.hpi.uni-potsdam.de> wrote:



Just in this case, we would rather need something like #copyAsMutable. Otherwise, we would make two copies ..

Are you sure? # shuffle is implemented in terms of #shuffleBy: which mutates in ace via #swap:with:

________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Eliot Miranda <eliot.miranda at gmail.com>
Gesendet: Mittwoch, 12. Februar 2020 18:37:20
An: The general-purpose Squeak developers list
Betreff: Re: [squeak-dev] [BUG] Shuffling an interval raises an error



On Feb 12, 2020, at 7:25 AM, Thiede, Christoph <Christoph.Thiede at student.hpi.uni-potsdam.de> wrote:



Code to reproduce:

(1 to: 10) shuffled.


Expected behavior:

output matches the condition [output sameElements: (1 to: 10)].


Actual behavior:

Error: You cannot store into interval!


Considerations:

The same problem also exists for: Symbol, ShortRunArray, and SortedCollection which are not modifiable.

#shuffledBy: performs #shuffleBy: on a copy of the receiver.

We could instead use something like (self asModifiable) to ensure that #swap:with: does not fail.

This could basically be [self as: self species], but unfortunately, this is more than three times slower than #copy.

How would you think about:

asModifiable

    self class = self species

        ifTrue: [^ self].

    ^ self as: self species


In general, #swap:with: and so #shuffleBy: are only efficient for Arrays and OrderedCollections, but not for RunArrays or LinkedLists.

Should we maybe just convert the receiver into an array in #shuffledBy:? Is #shuffledBy: required to return an object of self species?

One could add asMutable or asMutableSequence that could also check if an object isReadonly. It would answer self for things that are already mutable (and hopefully soon literals won’t be mutable; we have the change set somewhere), and would answer a mutable array for other things.



Best,

Christoph


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200212/c4372ebf/attachment.html>


More information about the Squeak-dev mailing list