Re: [PHP-DEV] Support rewinding of generators

2020-02-26 Thread Stanislav Malyshev
Hi! > 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(). That is provided that: 1. The original arguments of the function can be "remembered" - those can be complex

Re: [PHP-DEV] Support rewinding of generators

2020-02-26 Thread Levi Morrison via internals
On Wed, Feb 26, 2020 at 4:47 AM Nikita Popov wrote: > > Hi internals, > > Generators currently do not support rewinding -- or rather, only support it > if the generator is at/before the first yield, in which case rewinding is a > no-op. > > Generators make it real breeze to implement primitives li

Re: [PHP-DEV] Support rewinding of generators

2020-02-26 Thread Rowan Tommins
On Wed, 26 Feb 2020 at 15:23, Nikita Popov wrote: > Point of order: foreach() always rewinds the array / Iterator it gets. The > code does work correctly under the proposed scheme. > Ah, I'd missed that, sorry; that makes the entire first half of my e-mail irrelevant. :) The proposed behaviou

Re: [PHP-DEV] Support rewinding of generators

2020-02-26 Thread Nikita Popov
On Wed, Feb 26, 2020 at 4:03 PM Rowan Tommins wrote: > Hi Nikita, > > On 26 February 2020 11:47:14 GMT+00:00, Nikita Popov > wrote: > >There is a relatively simple (at least conceptually) way to make > generators > >rewindable: Remember the original arguments of the function, and basically > >"r

Re: [PHP-DEV] Support rewinding of generators

2020-02-26 Thread Rowan Tommins
Hi Nikita, On 26 February 2020 11:47:14 GMT+00:00, Nikita Popov 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 go

Re: [PHP-DEV] Support rewinding of generators

2020-02-26 Thread Judah Wright
Perhaps an easy userland implementation could be type-hinting a new generator type, to indicate that the generator should be rewindable by simply re-calling the function? // Safe to rewind function fooRange(int $from, int $to): RewindableGenerator { for ($i = $from; $i <= $to; $i++) { yield

[PHP-DEV] Support rewinding of generators

2020-02-26 Thread Nikita Popov
Hi internals, Generators currently do not support rewinding -- or rather, only support it if the generator is at/before the first yield, in which case rewinding is a no-op. Generators make it real breeze to implement primitives like function map(callable $function, iterable $iterable): \Iterator