Re: Support rewinding of generators
| From: | Nikita Popov | Date: | Wed, 26 Feb 2020 15:22:51 +0000 |
| Subject: | Re: Support rewinding of generators | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108770@lists.php.net to get a copy of this message | ||
On Wed, Feb 26, 2020 at 4:03 PM Rowan Tommins <rowan.collins@gmail.com>
wrote:
> Hi Nikita,
>
> On 26 February 2020 11:47:14 GMT+00:00, Nikita Popov
> <nikita.ppv@gmail.com> wrote:
> >There is a relatively simple (at least conceptually) way to make
> generators
> >rewindable: Remember the original arguments of the function, and basically
> >"re-invoke" it on rewind().
>
> This is an interesting idea.
>
> There is a gotcha though, neatly demonstrated by your example:
>
> >function map(callable $function, iterable $iterable): \Iterator {
> > foreach ($iterable as $key => $value) {
> > yield $key => $function($value);
> > }
> >}
>
> If the $iterable passed in is anything other than an array,
> re-invoking the function won't actually rewind it.
Point of order: foreach() always rewinds the array / Iterator it gets. The
code does work correctly under the proposed scheme.
Nikita