Re: Regarding array_shift()/array_unshift()
| From: | Larry Garfield | Date: | Wed, 02 Jun 2021 23:47:29 +0000 |
| Subject: | Re: Regarding array_shift()/array_unshift() | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-114701@lists.php.net to get a copy of this message | ||
On Wed, Jun 2, 2021, at 6:15 PM, Mike Schinkel wrote:
> > On Jun 2, 2021, at 7:07 PM, Larry Garfield <larry@garfieldtech.com> wrote:
> >
> > On Wed, Jun 2, 2021, at 5:59 PM, Mike Schinkel wrote:
> >>> On Jun 2, 2021, at 4:25 AM, Jordi Boggiano <j.boggiano@seld.be> wrote:
> >
> >>> IMO for unshift() it'd be fine to return a new array, but when processing a
> >>> list of things in a FIFO pattern I often used array_shift() to get the first "job" out of
> >>> the array. If $array->shift() returns a new array then how do I access the first item?
> >>
> >> That is a really excellent point, something I did not think to consider
> >> given how rarely I use array_shift().
> >>
> >> But your comment causes me to ponder a number of follow up questions,
> >> some of which are tangential. If any reader feels these tangents are
> >> worth discussing please make another email and quote my relevant
> >> comments so we can have a dedicated thread for each.
> >>
> >> 1.) Given Nikita's position that it would only be viable to offer a
> >> syntax that simulates method calling for arrays if the methods
> >> themselves are immutable can you envision a solution for allowing
> >> $array->shift() functionality that would address getting both element
> >> and shifted array without resorting to by-reference parameters?
> >
> > $list->head() (returns first item)
> > $list->tail() (returns all items but the first one)
>
> I assume $list->tail() would still have the O(<n>) problem in PHP, though?
>
> -Mike
If implemented like an array, yes. If implemented in a more efficient way in C, potentially not.
Or at least a smaller constant multiplier with n. (If something is O(n) but the cost per in is very
small, then it being O(n) is not a big deal. If the cost per n is huge, it's a very huge
deal.)
--Larry Garfield