Re: [pr-645] Add offset to reset() and end()

From: Date: Thu, 24 Apr 2014 03:28:01 +0000
Subject: Re: [pr-645] Add offset to reset() and end()
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-73770@lists.php.net to get a copy of this message
Hi Peter, On Sat, Apr 19, 2014 at 7:50 AM, Peter Cowburn <petercowburn@gmail.com>wrote: > > > > On 18 April 2014 09:58, Nikita Popov <nikita.ppv@gmail.com> wrote: > >> On Fri, Apr 18, 2014 at 2:39 AM, Tjerk Meesters <tjerk.meesters@gmail.com >> >wrote: >> >> > Hi, >> > >> > I've added a PR that satisfies #31375. >> > >> > https://github.com/php/php-src/pull/645 >> > >> > It adds a second optional argument to reset() and end() to specify the >> > number of elements that should be skipped (forward or backward). >> > > The last time that I can remember discussion of something like > array_seek() [1]–which is what this seems like it actually wants to be–the > reception was fairly positive, but the idea just fizzled out. I’d > recommend looking back over the archives for other similar discussions. > Thanks for digging up the earlier discussion; taking also into account the other feedback I have received, I have updated the PR: https://github.com/php/php-src/pull/645 It takes some cues from the earlier array_seek() proposal but: 1. doesn't update the internal pointer if seek() failed 2. returns boolean result to indicate success / failure 3. is not called array_seek() ;-) I would be perfectly okay if this gets shut down as well, but at least I've tried :) Example of "iterate over last 50 array entries": if (array_seek($array, -50, SEEK_END)) { while ($data = each($array)) { ... } } > I would be -1 on changing reset() and end() to make them be able to not > reset the array pointer, or not move to the end, respectively; especially > where a neater (IMO) alternative has already been discussed. > > [1] http://markmail.org/thread/sczgritz5jcccnwh [PHP-DEV] > array_seek > function > > > >> > >> > Currently, negative offsets are ignored. Should we raise a notice for >> such >> > cases? Something like: >> > >> > "Negative offsets are not supported." >> > >> > Any feedback is welcome. Thanks! >> > >> >> The reset() part seems to already be adequately covered by LimitIterator.. >> Personally I'd prefer not to add any new functionality to >> reset/current/... >> because you oughtn't be using those functions anyway. Trying to do manual >> array iteration with them is both somewhat unsafe (think nested iteration) >> and makes for really ugly code. >> >> // skip 10 elements of $it. Can be an ArrayIterator, of course >> foreach (new LimitIterator($it, 10) as $k => $v) { ... } >> >> Nikita >> > > -- -- Tjerk

« previous php.internals (#73770) next »