Array#last_index

J

jzakiya

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
 
T

Trans

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.
 
F

Florian Groß

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?
 
D

David A. Black

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 :)


David
 
B

Bill Atkins

------=_Part_1362_18299710.1113064130243
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

=20
array.last returns the value of the last array element.
=20
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:
=20
class Array
def last_index; self.length - 1 end
end
=20
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.
=20
I would really like to see this method added to class Array.
It is a nice complement to Array#last, and aides performance.
=20
Jabari Zakiya
=20
=20


--=20
$stdout.sync =3D 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"

------=_Part_1362_18299710.1113064130243--
 
D

Dominik Bathon

Can you not just use -1 as the index?

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
 
T

Trans

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.
 
J

jzakiya

David said:
Hi --



a.size-1 :)


David

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. ;-)

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

Jabari
 
T

Trans

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.
 
S

Saynatkari

Le 10/4/2005 said:
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?

E
 
M

Martin DeMello

Saynatkari said:
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
 
T

Trans

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.
 
O

ornil1

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.
 
R

Robert Klemme

Trans said:
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
 
M

Martin DeMello

Robert Klemme said:
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
 
T

Trans

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.
 
J

jzakiya

Martin said:
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);
}
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
 
T

Trans

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top