Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99706 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 39807 invoked from network); 3 Jul 2017 15:27:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jul 2017 15:27:15 -0000 Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 84.19.169.162 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 84.19.169.162 mail.experimentalworks.net Received: from [84.19.169.162] ([84.19.169.162:59142] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E0/32-15131-1526A595 for ; Mon, 03 Jul 2017 11:27:13 -0400 Received: from kuechenschabe.fritz.box (ppp-46-244-168-155.dynamic.mnet-online.de [46.244.168.155]) by mail.experimentalworks.net (Postfix) with ESMTPSA id DA7395DE49; Mon, 3 Jul 2017 17:27:09 +0200 (CEST) Message-ID: <1499095625.19635.52.camel@schlueters.de> To: Andreas Hennings , internals@lists.php.net Date: Mon, 03 Jul 2017 17:27:05 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.18.5.2-0ubuntu3.2 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] "Reader" as alternative to Iterator From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) 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 interface "Iterator", and a final class > "Generator" > (and others) that implement it. > > Using an iterator in foreach () is straightforward: > foreach ($iterator as $key => $value) {..} > > However, if we want to iterate only a portion and then continue > elsewhere at the position where we stopped, we need to do something > like this: > > for ($iterator->rewind(); $iterator->valid(); $iterator->next()) { >   $value = $iterator->current(); >   [..] > } > > This is unpleasantly verbose, and also adds performance overhead due > to additional function calls. Wouldn't SPL's NoRewindIterator be enough? $nit = new NoRewindIterator($it); foreach ($nit as $row) {  break; } foreach ($nit as $row) {   // continues same iteration } > > Also, manually writing an iterator is quite painful. > > I sometimes implement "readers" that can be used like this (*): > > // Gets a reader at position zero. > $reader = $readerProvider->getReader(); > while (FALSE !== $value = $reader->read()) { >   [..] > } > > (*) Note that I am using FALSE as an equivalent for "end of data". Of > course it would be nice if we had a dedicated constant for this, that > does not constrain the range of possible values of the iterator. That distinction is the reason why next() and valid() are different methods in iterators. johannes