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

Feature request: reverse method #13

Closed
maasha opened this issue Oct 28, 2011 · 6 comments
Closed

Feature request: reverse method #13

maasha opened this issue Oct 28, 2011 · 6 comments

Comments

@maasha
Copy link

maasha commented Oct 28, 2011

Can we please have a reverse method that efficiently reverses the order of elements inline.

Cheers,

Martin

@masa16
Copy link
Owner

masa16 commented Oct 28, 2011

This is achieved by the bracket method:
a[-1..0]

@maasha
Copy link
Author

maasha commented Oct 28, 2011

Yes, I found out. I was still surprised that there wasn't a reverse method. Ruby is about keeping surprises to a minimum, yes?

@maasha maasha closed this as completed Oct 28, 2011
@masa16
Copy link
Owner

masa16 commented Oct 28, 2011

I can see the natural behaviour of reverse method for one-dimensional array. But I havent found what a.reverse shoud return if a is a multi-dimensinal array.

@dicom
Copy link

dicom commented Oct 29, 2011

I like the convenience of a reverse method.

How about:

NArray#reverse(index=0)

On a one-dimensional array:

v = NArray.int(4).indgen!
[ 0, 1, 2, 3 ]
v.reverse
[ 3, 2, 1, 0 ]
v.reverse(0)
[ 3, 2, 1, 0 ]

Two-dimensional array:

arr = NArray.int(2,2).indgen!
[ [ 0, 1 ], 
  [ 2, 3 ] ] 
arr.reverse
[ [ 1, 0 ], 
  [ 3, 2 ] ] 
arr.reverse(0)
[ [ 1, 0 ], 
  [ 3, 2 ] ]
arr.reverse(1)
[ [ 2, 3 ], 
  [ 0, 1 ] ]  

And of course this principle works fine on higher dimensional arrays as well.

@masa16
Copy link
Owner

masa16 commented Oct 29, 2011

arr = NArray.int(2,2).indgen!
[ [ 0, 1 ], 
  [ 2, 3 ] ]

arr.reverse
[ [ 1, 0 ], 
  [ 3, 2 ] ] 

or

arr.reverse
[ [ 3, 2 ], 
  [ 1, 0 ] ] 

?

@dicom
Copy link

dicom commented Oct 29, 2011

How about:

NArray#reverse(index=true)

arr = NArray.int(2,2).indgen!
[ [ 0, 1 ], 
  [ 2, 3 ] ]

Without argument, all dimensions are reversed:

arr.reverse
[ [ 3, 2 ], 
  [ 1, 0 ] ]

If an index/dimension is specified, only the specified dimension gets reversed.
Reversing rows:

arr.reverse(0)
[ [ 1, 0 ], 
  [ 3, 2 ] ]

Reversing columns:

arr.reverse(1)
[ [ 2, 3 ], 
  [ 0, 1 ] ]  

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

3 participants