Array#last_index

array.last returns the value of the last array element.

In many instances it is necessary to get the index value
of the last array element. This is necessary in many
sorting and searching algorithms, especially where the
arrays are dynamically changing in size. You can do it like:

class Array
  def last_index; self.length - 1 end
end

But this is slower than necessary, because array.last
already computes/knows what the last index value is,
and thus the mechanism already exists to get this info.

I would really like to see this method added to class Array.
It is a nice complement to Array#last, and aides performance.

Jabari Zakiya

Jabari,

I agree that would good to have. Also I would prefer the method name to
be very short. Do you have any ideas for this?

T.

jzakiya@mail.com wrote:

array.last returns the value of the last array element.

In many instances it is necessary to get the index value
of the last array element. This is necessary in many
sorting and searching algorithms, especially where the
arrays are dynamically changing in size. You can do it like:

class Array
  def last_index; self.length - 1 end
end

But this is slower than necessary, because array.last
already computes/knows what the last index value is,
and thus the mechanism already exists to get this info.

I would really like to see this method added to class Array.
It is a nice complement to Array#last, and aides performance.

Can you not just use -1 as the index?

I don't think this should be all that slow. Ruby tracks the array's length
as it grows, so all you're doing here is reading a variable from memory and
subtracting one. Still, it would probably be a good idea to add this to
Array.

···

On Apr 9, 2005 12:49 AM, jzakiya@mail.com <jzakiya@mail.com> wrote:

array.last returns the value of the last array element.

In many instances it is necessary to get the index value
of the last array element. This is necessary in many
sorting and searching algorithms, especially where the
arrays are dynamically changing in size. You can do it like:

class Array
def last_index; self.length - 1 end
end

But this is slower than necessary, because array.last
already computes/knows what the last index value is,
and thus the mechanism already exists to get this info.

I would really like to see this method added to class Array.
It is a nice complement to Array#last, and aides performance.

Jabari Zakiya

--
$stdout.sync = true
"Just another Ruby hacker.".each_byte do |b|
('a'..'z').step do|c|print c+"\b";sleep 0.007 end;print b.chr
end; print "\n"

Hi --

···

On Sun, 10 Apr 2005, Trans wrote:

Jabari,

I agree that would good to have. Also I would prefer the method name to
be very short. Do you have any ideas for this?

a.size-1 :slight_smile:

David

--
David A. Black
dblack@wobblini.net

Here are some examples (in case you don't know how that works):

irb(main):083:0> [1, 2, 3, 4, 5][-1]
=> 5
irb(main):084:0> [1, 2, 3, 4, 5][-2]
=> 4
irb(main):085:0> [1, 2, 3, 4, 5][2..-1]
=> [3, 4, 5]
irb(main):086:0> [1, 2, 3, 4, 5][2..-2]
=> [3, 4]

And so on.

Dominik

···

On Sat, 09 Apr 2005 18:26:15 +0200, Florian Groß <florgro@gmail.com> wrote:

jzakiya@mail.com wrote:

array.last returns the value of the last array element.
In many instances it is necessary to get the index value
of the last array element. This is necessary in many
sorting and searching algorithms, especially where the
arrays are dynamically changing in size. You can do it like:
class Array
  def last_index; self.length - 1 end
end
But this is slower than necessary, because array.last
already computes/knows what the last index value is,
and thus the mechanism already exists to get this info.
I would really like to see this method added to class Array.
It is a nice complement to Array#last, and aides performance.

Can you not just use -1 as the index?

Can you not just use -1 as the index?

No. Because, it is not always simply a matter of getting to the last
element. If it were then #last itself would suffice. The use case here
is when the last index is needed.

T.

David A. Black wrote:

Hi --

> Jabari,
>
> I agree that would good to have. Also I would prefer the method

name to

> be very short. Do you have any ideas for this?

a.size-1 :slight_smile:

David

--
David A. Black
dblack@wobblini.net

These maybe: a.ndx a.endx a.lastndx a.last_i

I tend to like the last two the best because they are
closer to a.last_index, which is so POLS. :wink:

If not a.last_index I would go with a.last_i

Jabari

···

On Sun, 10 Apr 2005, Trans wrote:

"Trans" <transfire@gmail.com> schrieb im Newsbeitrag
news:1113074095.070106.22910@l41g2000cwc.googlegroups.com...

> Can you not just use -1 as the index?

No. Because, it is not always simply a matter of getting to the last
element. If it were then #last itself would suffice. The use case here
is when the last index is needed.

Care to unveil the use case? I never neede that myself - just
wondering...

Kind regards

    robert

Thanks Jabari!

Array#last_index is now apart of Ruby Facets. Your suggestions for a
shorter name helped me figure out a nice alias too: #nth.

T.

I've written C code that started with a pointer at either end of an
array and walked them towards each other.

martin

···

Robert Klemme <bob.news@gmx.net> wrote:

"Trans" <transfire@gmail.com> schrieb im Newsbeitrag
news:1113074095.070106.22910@l41g2000cwc.googlegroups.com...
> > Can you not just use -1 as the index?
>
> No. Because, it is not always simply a matter of getting to the last
> element. If it were then #last itself would suffice. The use case here
> is when the last index is needed.

Care to unveil the use case? I never neede that myself - just
wondering...

Thanks Jabari!

Array#last_index is now apart of Ruby Facets. Your suggestions for a
shorter name helped me figure out a nice alias too: #nth.

The abbreviation 'nth' is often used to indicate a certain
unknown index position 'n' and may thus be confusing?

T.

E

···

Le 10/4/2005, "Trans" <transfire@gmail.com> a écrit:

--
No-one expects the Solaris POSIX implementation!

Martin DeMello wrote:

>
> "Trans" <transfire@gmail.com> schrieb im Newsbeitrag
> news:1113074095.070106.22910@l41g2000cwc.googlegroups.com...
> > > Can you not just use -1 as the index?
> >
> > No. Because, it is not always simply a matter of getting to the

last

> > element. If it were then #last itself would suffice. The use case

here

> > is when the last index is needed.
>
> Care to unveil the use case? I never neede that myself - just
> wondering...

I've written C code that started with a pointer at either end of an
array and walked them towards each other.

martin

Here's the source code from Array.c for Array#last:

static VALUE
rb_ary_last(argc, argv, ary)
    int argc;
    VALUE *argv;
    VALUE ary;
{
    if (argc == 0) {
  if (RARRAY(ary)->len == 0) return Qnil;
  return RARRAY(ary)->ptr[RARRAY(ary)->len-1];
    }
    else {
  VALUE nv, result;
  long n, i;

  rb_scan_args(argc, argv, "01", &nv);
  n = NUM2LONG(nv);
  if (n > RARRAY(ary)->len) n = RARRAY(ary)->len;
  result = rb_ary_new2(n);
  for (i=RARRAY(ary)->len-n; n--; i++) {
      rb_ary_push(result, RARRAY(ary)->ptr[i]);
  }
  return result;
    }
}

Here's my modification of it to produce Array#last_index.

static VALUE
rb_ary_last_index(argc, argv, ary)
    int argc;
    VALUE *argv;
    VALUE ary;
{
  VALUE nv, result;
  long n;

  rb_scan_args(argc, argv, "01", &nv);
  n = NUM2LONG(nv);
  if (n > RARRAY(ary)->len) n = RARRAY(ary)->len;
  return n;
}

Could someone verify/modify/test this to see if it works.

Jabari

···

Robert Klemme <bob.news@gmx.net> wrote:

I agree. Array#bound, perhaps, since the other bound is always zero
anyway.

martin

···

Saynatkari <ruby-ml@magical-cat.org> wrote:

Le 10/4/2005, "Trans" <transfire@gmail.com> a écrit:

>Thanks Jabari!
>
>Array#last_index is now apart of Ruby Facets. Your suggestions for a
>shorter name helped me figure out a nice alias too: #nth.

The abbreviation 'nth' is often used to indicate a certain
unknown index position 'n' and may thus be confusing?

The abbreviation 'nth' is often used to indicate a certain
unknown index position 'n' and may thus be confusing?

Hmmm... perhaps. I was thinking of it along the lines of the phrase
"zero to n". 'n' is unknown, and thus nth asks what is the nth
position? Granted, it's not perfect, but it is nice-and-short.

T.

Hi Jabari,

Here's my modification of it to produce Array#last_index.

static VALUE
rb_ary_last_index(argc, argv, ary)
    int argc;
    VALUE *argv;
    VALUE ary;
{
  VALUE nv, result;
  long n;

  rb_scan_args(argc, argv, "01", &nv);
  n = NUM2LONG(nv);
  if (n > RARRAY(ary)->len) n = RARRAY(ary)->len;
  return n;
}

Could someone verify/modify/test this to see if it works.

I'm not expert on these matters, to be sure, but if you look at the
length method you'll see this:

static VALUE
rb_ary_length(ary)
    VALUE ary;
{
    return LONG2NUM(RARRAY(ary)->len);
}

So a simpler solution for last_index is:

  static VALUE
  rb_ary_last_index(ary)
      VALUE ary;
  {
      if (RARRAY(ary)->len == 0) return Qnil;
      return LONG2NUM(RARRAY(ary)->len-1);
  }

The rb_scan_args isn't needed since this method doesn't take any
arguments (let alone any number of them whihc is what that is generally
for).

Thank you for offering this code, BTW. I've added it to the Suby
project.

Thanks,
T.

Martin DeMello wrote:

>
>
> >Thanks Jabari!
> >
> >Array#last_index is now apart of Ruby Facets. Your suggestions for

a

> >shorter name helped me figure out a nice alias too: #nth.
>
> The abbreviation 'nth' is often used to indicate a certain
> unknown index position 'n' and may thus be confusing?

I agree. Array#bound, perhaps, since the other bound is always zero
anyway.

martin

Following in the same spirit: Array#max_i Array#i_max

Jabari

···

Saynatkari <ruby-ml@magical-cat.org> wrote:
> Le 10/4/2005, "Trans" <transfire@gmail.com> a écrit:

Hmmm... perhaps. I was thinking of it along the lines of the phrase
"zero to n". 'n' is unknown, and thus nth asks what is the nth
position? Granted, it's not perfect, but it is nice-and-short.

Please choose another name, it's very confusing. In Lisp there's a
function called "nth" and it takes a number and a list, returning the
nth element of a list. See, for example here:
http://www.delorie.com/gnu/docs/emacs-lisp-intro/emacs-lisp-intro_117.html

So any Lisp hacker would assume wrong semantics for it.

Please choose another name, it's very confusing. In Lisp there's a...

Okay, I will. I have #last_index. I was just hoping to get a nice,
_very short_ alias, which is fitting to its use cases (in my opinion).

Thanks,
T.