Skip Menu |
Report information
Id: 126799
Status: resolved
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: alex.jakimenko [at] gmail.com
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



Subject: Reversing, rotating, sorting fixed-dimension arrays
To: rakudobug [...] perl.org
Date: Fri, 4 Dec 2015 04:14:17 +0200
From: Aleks-Daniel Jakimenko-Aleksejev <alex.jakimenko [...] gmail.com>
Download (untitled) / with headers
text/plain 1.5k
Code:
my @a[3]; say @a.reverse

Result:
Cannot reverse a fixed-dimension array
  in block <unit> at -e:1


It may be more complicated for multi-dimensional arrays, but reversing a 1-dimensional is rather straightforward. Therefore, it should probably throw a NYI warning.

But then it gets weirder:
my @a[3] = <5 10 15>; say reverse(@a)

Result:
[15 10 5]

Whoops! Seems like in this case there is a difference between 「reverse」 sub and 「.reverse」 method. I am not sure it if should be like so, I've always thought that both should behave almost identically.

Let's try the same thing with 「sort」:
my @a[2;3] = (15,16,17), (4,5,6); say sort(@a)

Since 「reverse」 sub worked, we could expect 「sort」 to work as well (but notice that there are two dimensions now!).
Here is the result:
Cannot access 2 dimension array with 1 indices
  in block <unit> at -e:1

Okay, that's LTA but it was somewhat expected. It is also how 「reverse」 works with multidimensional arrays. Anyway, let's try the method:
my @a[2;3] = (15,16,17), (4,5,6); say @a.sort

I am kinda expecting either NYI (if sorting a multidim array will ever make any sense) or “Cannot reverse a fixed-dimension array” error. Let's see.
Result:
(4 5 6 15 16 17)


It is just too inconsistent, I think.

Possible solution:
1) Same behavior for subs and methods
2) NYI warning for all operations on 1-dimensional arrays if not implemented.
3) “Cannot reverse a multidimensional array” and similar errors for all operations with arrays that have more than 1 dimension.
RT-Send-CC: perl6-compiler [...] perl.org
Download (untitled) / with headers
text/plain 2.6k
On Thu Dec 03 18:14:29 2015, alex.jakimenko@gmail.com wrote: Show quoted text
> Code: > my @a[3]; say @a.reverse > > Result: > Cannot reverse a fixed-dimension array > in block <unit> at -e:1 > > > It may be more complicated for multi-dimensional arrays, but reversing a > 1-dimensional is rather straightforward. Therefore, it should probably > throw a NYI warning. >
I decided to just implement it for 1-dimensional arrays. Left it as "no you cannot" for 2 dimensions and up, since it's not clear we'll settle on a semantics for that. Similar story for rotate, which now works on single-dim. Show quoted text
> But then it gets weirder: > my @a[3] = <5 10 15>; say reverse(@a) > > Result: > [15 10 5] > > Whoops! Seems like in this case there is a difference between 「reverse」 sub > and 「.reverse」 method. I am not sure it if should be like so, I've always > thought that both should behave almost identically. >
Fixed that one now, and along the way added a load of missing tests for sub forms of things on multi-dim arrays to make sure there weren't a bunch of other inconsistencies. Show quoted text
> Let's try the same thing with 「sort」: > my @a[2;3] = (15,16,17), (4,5,6); say sort(@a) > > Since 「reverse」 sub worked, we could expect 「sort」 to work as well (but > notice that there are two dimensions now!). > Here is the result: > Cannot access 2 dimension array with 1 indices > in block <unit> at -e:1 >
This is now fixed to produce (4,5,6,15,16,17) - that is, working on the leaves. Most things (like map) already worked nicely this way in both sub or method form, but sort does some odd things to cope with the first thing it gets being a Callable. Show quoted text
> Okay, that's LTA but it was somewhat expected. It is also how 「reverse」 > works with multidimensional arrays. Anyway, let's try the method: > my @a[2;3] = (15,16,17), (4,5,6); say @a.sort > > I am kinda expecting either NYI (if sorting a multidim array will ever make > any sense) or “Cannot reverse a fixed-dimension array” error. Let's see. > Result: > (4 5 6 15 16 17) >
Which was correct already, and there's no a test to make sure we don't break it. Show quoted text
> It is just too inconsistent, I think. >
Yeah, the sub forms wanted a look, though most of the others were in good shape already. Show quoted text
> Possible solution: > 1) Same behavior for subs and methods > 2) NYI warning for all operations on 1-dimensional arrays if not > implemented. > 3) “Cannot reverse a multidimensional array” and similar errors for all > operations with arrays that have more than 1 dimension.
Pretty much did that, though instead of 2 I just implemented the missing reverse/rotate operations. :-) Tests in S09-multidim/methods.t and the newly-added S09-multidim/subs.t.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org