[BUG] Shuffling an interval raises an error

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[BUG] Shuffling an interval raises an error

Christoph Thiede

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?


Best,

Christoph



Reply | Threaded
Open this post in threaded view
|

Re: [BUG] Shuffling an interval raises an error

Eliot Miranda-2


On Feb 12, 2020, at 7:25 AM, Thiede, Christoph <[hidden email]> 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




Reply | Threaded
Open this post in threaded view
|

Re: [BUG] Shuffling an interval raises an error

Christoph Thiede

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


Von: Squeak-dev <[hidden email]> im Auftrag von Eliot Miranda <[hidden email]>
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 <[hidden email]> 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




Reply | Threaded
Open this post in threaded view
|

Re: [BUG] Shuffling an interval raises an error

Eliot Miranda-2
Hi Christoph,

On Feb 12, 2020, at 9:45 AM, Thiede, Christoph <[hidden email]> 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 <[hidden email]> im Auftrag von Eliot Miranda <[hidden email]>
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 <[hidden email]> 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





Reply | Threaded
Open this post in threaded view
|

Re: [BUG] Shuffling an interval raises an error

Christoph Thiede

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 <[hidden email]> im Auftrag von Eliot Miranda <[hidden email]>
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 <[hidden email]> 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 <[hidden email]> im Auftrag von Eliot Miranda <[hidden email]>
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 <[hidden email]> 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