Giter Club home page Giter Club logo

Comments (6)

masa16 avatar masa16 commented on June 26, 2024

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

from narray.

maasha avatar maasha commented on June 26, 2024

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

from narray.

masa16 avatar masa16 commented on June 26, 2024

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.

from narray.

dicom avatar dicom commented on June 26, 2024

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.

from narray.

masa16 avatar masa16 commented on June 26, 2024
arr = NArray.int(2,2).indgen!
[ [ 0, 1 ], 
  [ 2, 3 ] ]

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

or

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

?

from narray.

dicom avatar dicom commented on June 26, 2024

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 ] ]  

from narray.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.