Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reversing, rotating, sorting fixed-dimension arrays #4815

Closed
p6rt opened this issue Dec 4, 2015 · 4 comments
Closed

Reversing, rotating, sorting fixed-dimension arrays #4815

p6rt opened this issue Dec 4, 2015 · 4 comments

Comments

@p6rt
Copy link

p6rt commented Dec 4, 2015

Migrated from rt.perl.org#126799 (status was 'resolved')

Searchable as RT126799$

@p6rt
Copy link
Author

p6rt commented Dec 4, 2015

From @AlexDaniel

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.

@p6rt
Copy link
Author

p6rt commented Dec 13, 2015

From @jnthn

On Thu Dec 03 18​:14​:29 2015, alex.jakimenko@​gmail.com wrote​:

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.

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.

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.

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.

It is just too inconsistent, I think.

Yeah, the sub forms wanted a look, though most of the others were in good shape already.

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.

@p6rt
Copy link
Author

p6rt commented Dec 13, 2015

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Dec 13, 2015

@jnthn - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Dec 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant