Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99708 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42615 invoked from network); 3 Jul 2017 15:32:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jul 2017 15:32:08 -0000 Authentication-Results: pb1.pair.com header.from=me@kelunik.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=me@kelunik.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kelunik.com from 81.169.146.217 cause and error) X-PHP-List-Original-Sender: me@kelunik.com X-Host-Fingerprint: 81.169.146.217 mo4-p00-ob.smtp.rzone.de Received: from [81.169.146.217] ([81.169.146.217:29349] helo=mo4-p00-ob.smtp.rzone.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 93/D2-15131-6736A595 for ; Mon, 03 Jul 2017 11:32:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1499095923; l=4966; s=domk; d=kelunik.com; h=Content-Type:Cc:To:Subject:Date:From:References:In-Reply-To: MIME-Version; bh=skDa2/E2CQb+Mmghg1LE/Q52a2OouVy49TiofkpP3mI=; b=BJON1ffK/d+JNy33rqf+Y9bX8lfAMjHVwxE1AyyQMUYOjBw40hseC+ksYqsPWY2fke OfsiBb8BFMNp0diZzcBKuEE56EndFwP/J9aWVI/PvtCoGB5bCG+AWDhkYQNHElNTHFdK cFuenL3rPkb6kUo9TXE44MJmjG3S+BguWzyqs= X-RZG-AUTH: :IWkkfkWkbvHsXQGmRYmUo9mls2vWuiu+7SLDup6E67mzuoNHBqX93Q== X-RZG-CLASS-ID: mo00 Received: by mail-oi0-f45.google.com with SMTP id l130so84223597oib.1 for ; Mon, 03 Jul 2017 08:32:03 -0700 (PDT) X-Gm-Message-State: AKS2vOwYchu7SuPLNLhlwubEK99DVgB+cItFgNRjLbOB3zK+rg2TRnEm PlcFESvH/1M4w2JgJiTEvwSOVRmheg== X-Received: by 10.202.7.70 with SMTP id 67mr18569012oih.184.1499095923026; Mon, 03 Jul 2017 08:32:03 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.81.135 with HTTP; Mon, 3 Jul 2017 08:32:02 -0700 (PDT) In-Reply-To: <1499095625.19635.52.camel@schlueters.de> References: <1499095625.19635.52.camel@schlueters.de> Date: Mon, 3 Jul 2017 17:32:02 +0200 X-Gmail-Original-Message-ID: Message-ID: To: =?UTF-8?Q?Johannes_Schl=C3=BCter?= Cc: Andreas Hennings , PHP Internals Content-Type: multipart/alternative; boundary="94eb2c13e51ef9f51f05536b7715" Subject: Re: [PHP-DEV] "Reader" as alternative to Iterator From: me@kelunik.com (Niklas Keller) --94eb2c13e51ef9f51f05536b7715 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 2017-07-03 17:27 GMT+02:00 Johannes Schl=C3=BCter : > 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 =3D> $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 =3D $iterator->current(); > > [..] > > } > > > > This is unpleasantly verbose, and also adds performance overhead due > > to additional function calls. > > Wouldn't SPL's NoRewindIterator be enough? > > $nit =3D 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 =3D $readerProvider->getReader(); > > while (FALSE !=3D=3D $value =3D $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. Not really, Iterator::next() returns void, so could as well return bool. Regards, Niklas --94eb2c13e51ef9f51f05536b7715--