in reply to Re: Dealing with Lists and Arrays
in thread Dealing with Lists and Arrays
It's not only a matter of merely simulating the behavior of shift and unshift, it may have a lot to do with execution efficiency since a reverse is an expensive operation. It involves creating a new list and populating it with the elements of the original in O(n) time (n = length of the list).
A shift is typically O(1) time since it involves just taking out the first element of which you have the address anyway -- it's the start of the list -- and making the second element the first. The same holds for unshift which is a simple insertion at the beginning of the list.
So for reasons of efficiency, you really want shift and unshift, not only to make code look more elegant.
Hope this helps, -gjb-
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Dealing with Lists and Arrays
by broquaint (Abbot) on Nov 11, 2002 at 15:10 UTC |