On Wed, Feb 26, 2020 at 4:03 PM Rowan Tommins <rowan.coll...@gmail.com>
wrote:

> Hi Nikita,
>
> On 26 February 2020 11:47:14 GMT+00:00, Nikita Popov
> <nikita....@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

Reply via email to