--- Eric Hodel <drbrain / segment7.net> wrote:

> > As far as I can tell, all of these should be equivalent:
> >
> > array.shift
> > array.delete_at(0)
> > array.slice!(0)

> > Are you saying these don't have the same functionality?
> 
> They take different arguments, so must parse their arguments 
> 
> differently and behave differently based on that.  I don't
> see any  
> reason to complicate matters to special case for rarely-used 
> 
> behaviors.  There's no reason to write arr.delete_at 0 when
> you could  
> write arr.shift.

If you wanted to delete 5 elements from the beginning of an
array (a big one), you would think that your best bet would be
arr.slice!(0,5), right?   For performance, 5 arr.shift is much
faster - 1 O(N) ops vs. 5 O(N).

There is no reason why slice! couldn't make the shift
optimization when dealing with elements at the beginning of the
array.

I also noticed that if you do bunch of shifts (which seem to be
O(1)), the same number of unshifts are also fast (O(1)).  But,
when you get back to where you started, the unshifts become
O(N).  It is unfortunate that the implementation doesn't try to
stretch the array to the left just like it does to the right to
make all unshifts O(1).  And then of course all the equivalents
should make the same optimization.

> > OT... does anybody else hate it that the String and Array
> API's
> > have suttle differences and lack methods of the other?
> 
> No, I don't notice it at all.  Strings aren't Arrays and
> Arrays  
> aren't Strings.  They only look the same if you squint really
> hard.

I'm writing some general classes that equally apply to Strings
and Arrays.  Unfortunately, I can't take advantage of
Array#shift because String doesn't have it.  I use the least
common denominators: []/slice, []=, slice!, <<, and
size/length.  If they had more in common I'd like to use those.



		
__________________________________ 
Yahoo! Mail 
Stay connected, organized, and protected. Take the tour: 
http://tour.mail.yahoo.com/mailtour.html