Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99806 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 45088 invoked from network); 7 Jul 2017 05:21:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Jul 2017 05:21:40 -0000 Authentication-Results: pb1.pair.com smtp.mail=me@daveyshafik.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=me@daveyshafik.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain daveyshafik.com from 209.85.216.179 cause and error) X-PHP-List-Original-Sender: me@daveyshafik.com X-Host-Fingerprint: 209.85.216.179 mail-qt0-f179.google.com Received: from [209.85.216.179] ([209.85.216.179:34580] helo=mail-qt0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EB/AC-47109-06A1F595 for ; Fri, 07 Jul 2017 01:21:37 -0400 Received: by mail-qt0-f179.google.com with SMTP id 32so19149719qtv.1 for ; Thu, 06 Jul 2017 22:21:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daveyshafik-com.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=AqMKhfLlYgp8mmjD5QIn1uMYNlnNoXhi/sQNarCPhAo=; b=Va3xcnopCTgXrXMhoa8x+jI6NtzMsCvlO1MAvqaJtxS8bZRrdNEiplqNlGEtURvpwM dxgdwRdNTGKCYfuR4GoJdmzsJ/7UkVGEMohBRRPYvuNNY0uxsJOSNoT5pt0sK2SssTzD emtPLUQq6qGMv7I0JlUXlJBcuBLxVJhjByyO7k89GZzgJYrB7N+PMaSvjRrDWunagbkF tGh3CshhUH0wCpwv2Mqp0broV/Nt2dXcVQIn/5QwUYSEzwmHeyzh+aCV3KkMhn413I5y rx938AXcFi6t3EONr7s2vot1lEV6AfoABDVrwqEZ7aPHew+pGSzGRAz+TIgOka+hehFK WABw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=AqMKhfLlYgp8mmjD5QIn1uMYNlnNoXhi/sQNarCPhAo=; b=apVSdc51fxTfG/2fWrVkszdrkrvmQ9juZCF5v7iAetTJPBCAv111PXsPZCk/ARL3YE VVqWCqgSeXe5gi0w3CwFUrrPLq68Y7bAMOiZqvnb+Mfb0zK2TBdKewDH5IKP9GI08yx2 xxYT94mmjIJpyYK4jBpAtUUnNeAykLxgn9i6pRKdrB++o5uj7pzi93nmorrhsJPYW+Dt /26MljHppeq6PujQprZcICZhS0cjRYpS+GuaiJJmF8ELkewvcNSjknPp9XGsBw+J7ScC 6jLQ3/NscArqEGE7GXNfbMVqPMGPhCPUKIciGcxYsVB/nakLWsQmtMsD9h3j6kQrg9C5 lXUw== X-Gm-Message-State: AIVw110tqrQvvkZL3UzaqIEVy36QIQqmEyWwyLj7RghLUxRaxgLhie7G K7INw7JStReMOhLWP+zfbwObfYDQjK15 X-Received: by 10.237.46.225 with SMTP id k88mr13790621qtd.5.1499404894408; Thu, 06 Jul 2017 22:21:34 -0700 (PDT) MIME-Version: 1.0 Sender: me@daveyshafik.com Received: by 10.200.50.4 with HTTP; Thu, 6 Jul 2017 22:21:33 -0700 (PDT) In-Reply-To: References: <1499095625.19635.52.camel@schlueters.de> Date: Thu, 6 Jul 2017 22:21:33 -0700 X-Google-Sender-Auth: myviZ8A5FbapOCA6ceaqlf0QIM0 Message-ID: To: Rowan Collins Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="94eb2c122b361b26f90553b36899" Subject: Re: [PHP-DEV] "Reader" as alternative to Iterator From: davey@php.net (Davey Shafik) --94eb2c122b361b26f90553b36899 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I believe the correct solution to this is an OuterIterator =E2=80=94 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 own OuterIterators, which implements whatever logic your Reader should have. OuterIterators are also compose-able, you can stack them, though of course there are performance considerations here. I would say that any alternative to OuterIterator's should have a clear and demonstrable benefit in both developer experience, and performance before it should be considered. - Davey On Wed, Jul 5, 2017 at 3:51 AM, Rowan Collins wrote: > 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 is implemented as > >reader classes, and some other things are implemented as iterator > >classes. > > > >Then for the actual operational code, I need to choose to use either > >iterator syntax or reader syntax. So either I need adapters for all > >iterators, or I need adapters for all readers. > > An alternative to adapters in this case might be a Trait that allows you > to write the class as a Reader, but turn it into a fully-functioning > Iterator with one line of code. If you always call your read method read(= ) > you only need a single adapter which calls that at the appropriate times > and buffers the value to know if it's reached the end. That adapter could > be "pasted" by a Trait into each class where read() is defined, and read(= ) > could even be private. > > Regards, > > -- > Rowan Collins > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > --94eb2c122b361b26f90553b36899--