Re: Allow Iterator to be used with current, next, reset, key functions
| From: | Larry Garfield | Date: | Mon, 31 Oct 2016 21:49:01 +0000 |
| Subject: | Re: Allow Iterator to be used with current, next, reset, key functions | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-96684@lists.php.net to get a copy of this message | ||
On 10/31/2016 01:07 PM, Levi Morrison wrote:
On Mon, Oct 31, 2016 at 11:57 AM, Levi Morrison <levim@php.net> wrote:Might it make sense then to have first(iterable) first_key(iterable) array_last(array) array_last_key(array) It's not quite as parallel, but it does provide more broadly compatible functionality. If we're concerned about pw0ning the "first" function name, perhaps it's time to start a new prefix: iter_first(iterable) iter_first_key(iterable) array_last(array) array_last_key(array) Which gives a natural place to start putting iterable-friendly alternates of the various array_* functions, for those that make sense. (think iter_fold(), iter_map(), iter_filter(), etc.) (Insert debate about parameter order and method-based alternatives here.) --Larry GarfieldOn Mon, Oct 31, 2016 at 11:27 AM, Larry Garfield <larry@garfieldtech.com> wrote:Additionally consuming the iterator would be an algorithmic complexity of O(n) whereas for an array would just be O(1). So not only is it annoying that the iterator is consumed and can't be guaranteed to be rewound it is also costly. The first and first-key routines do not have these issues.Question: Now that we have "iterable" as a type, should we be adding more array-specific functions or should such operations be designed, and named, to operate on any iterable? (first(), first_key(), etc.)At least for last and last-key I do not think we should make them work with any iterable. My reasoning is that we do not want to consume the iterator for them because many iterators are not rewindable (such as generators). The first and first-key routines should generally work becauserewind(); valid(); current(); rewind();doesn't contain anext()call. Rewinding without iterating works for generators: https://3v4l.org/O7ZKO Rewinding after a next() call: https://3v4l.org/QI434