Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-06 Thread Davey Shafik
I believe the correct solution to this is an OuterIterator — an OuterIterator contains the logic to constrain the loop, for example, we have the LimitIterator, FilterIterator, RegexIterator, etc. See this example: http://php.net/manual/en/class.limititerator.php#example-4473 You can build your

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-05 Thread Rowan Collins
On 3 July 2017 16:58:16 BST, Andreas Hennings wrote: >My motivation was to be able to use iterators and readers >interchangeably. >Readers are easier to implement as classes. >Iterators have the benefit of the generator syntax. >The idea is to have a library where some stuff

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Niklas Keller
2017-07-03 17:50 GMT+02:00 Andreas Hennings : > On Mon, Jul 3, 2017 at 9:09 AM, Niklas Keller wrote: > > > > Hey Andreas, > > > > what you're trying to do here seems to be premature optimization. While > you > > save a bunch of method calls, your I/O will

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Andreas Hennings
On Mon, Jul 3, 2017 at 6:08 PM, Andreas Hennings wrote: > On Mon, Jul 3, 2017 at 5:53 PM, Johannes Schlüter > wrote: >> On Mo, 2017-07-03 at 17:32 +0200, Niklas Keller wrote: >> >>> > That distinction is the reason why next() and valid() are different

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Andreas Hennings
On Mon, Jul 3, 2017 at 5:53 PM, Johannes Schlüter wrote: > On Mo, 2017-07-03 at 17:32 +0200, Niklas Keller wrote: > >> > That distinction is the reason why next() and valid() are different >> > methods in iterators. I would rather say this is the reason why current() and

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Andreas Hennings
On Mon, Jul 3, 2017 at 5:27 PM, Johannes Schlüter wrote: > > Wouldn't SPL's NoRewindIterator be enough? > > $nit = new NoRewindIterator($it); > > foreach ($nit as $row) { > break; > } > foreach ($nit as $row) { > // continues same iteration > } I had not noticed this

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Johannes Schlüter
On Mo, 2017-07-03 at 17:32 +0200, Niklas Keller wrote: > > That distinction is the reason why next() and valid() are different > > methods in iterators. > > Not really, Iterator::next() returns void, so could as well return > bool. Well, that story is a bit longer and I cut it short. Let's

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Andreas Hennings
On Mon, Jul 3, 2017 at 9:09 AM, Niklas Keller wrote: > > Hey Andreas, > > what you're trying to do here seems to be premature optimization. While you > save a bunch of method calls, your I/O will be the actual bottleneck there. > It's entirely fine to implement such logic in

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Andreas Hennings
Thanks everyone so far for the replies! I think I need to do some "homework", and come back with benchmarks and provide real examples. I remember that the overhead did make a difference in performance, but I should back that up with real data. For now just some inline replies. On Mon, Jul 3,

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Niklas Keller
2017-07-03 17:27 GMT+02:00 Johannes Schlüter : > On Sa, 2017-07-01 at 19:38 +0200, Andreas Hennings wrote: > > Hello internals, > > (this is my first email to this list, hopefully I'm doing ok.) > > > > --- >

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Johannes Schlüter
On Sa, 2017-07-01 at 19:38 +0200, Andreas Hennings wrote: > Hello internals, > (this is my first email to this list, hopefully I'm doing ok.) > > --- > - > > Background / motivation: > > Currently in PHP we have an

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Sara Golemon
On Sun, Jul 2, 2017 at 10:49 PM, Andreas Hennings wrote: > (I wanted dedicated reader classes for different return types, e.g. > one "RowReader", one "AssocReader", one "ObjectReader". So here I > would need one adapter class per type. But let's focus on the simple > case,

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-03 Thread Niklas Keller
2017-07-03 4:49 GMT+02:00 Andreas Hennings : > Well, on a current project I made an attempt to write different > adapters in userland. > I finally decided that the clutter is not worth. > So for this project I wrote everything as "readers", and not as iterators. > > With a

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-02 Thread Andreas Hennings
Well, on a current project I made an attempt to write different adapters in userland. I finally decided that the clutter is not worth. So for this project I wrote everything as "readers", and not as iterators. With a native solution, one could do this: function generate() { yield 'a'; yield

Re: [PHP-DEV] "Reader" as alternative to Iterator

2017-07-02 Thread Sara Golemon
On Sat, Jul 1, 2017 at 1:38 PM, Andreas Hennings wrote: > Hello internals, > (this is my first email to this list, hopefully I'm doing ok.) > Welcome to php-internals! > Establish a new interface in core, "Reader" or "ReaderInterface" (*). > This interface has only one

[PHP-DEV] "Reader" as alternative to Iterator

2017-07-02 Thread Andreas Hennings
Hello internals, (this is my first email to this list, hopefully I'm doing ok.) (I sent this message before, but I think it was rejected due to html/multipart and lack of [PHP-DEV] in subject) Background /

[PHP-DEV] "Reader" as alternative to Iterator

2017-07-02 Thread Andreas Hennings
Hello internals, (this is my first email to this list, hopefully I'm doing ok.) (I sent this message before, but I think it was rejected due to html/multipart) Background / motivation: Currently in PHP we have an

[PHP-DEV] "Reader" as alternative to Iterator

2017-07-02 Thread Andreas Hennings
Hello internals, (this is my first email to this list, hopefully I'm doing ok.) Background / motivation: Currently in PHP we have an interface "Iterator", and a final class "Generator" (and others) that implement