Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113063 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 34862 invoked from network); 3 Feb 2021 16:06:48 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Feb 2021 16:06:48 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 52CFD180503 for ; Wed, 3 Feb 2021 07:50:19 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 3 Feb 2021 07:50:18 -0800 (PST) Received: by mail-lf1-f41.google.com with SMTP id v24so34052782lfr.7 for ; Wed, 03 Feb 2021 07:50:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=nEh/b2LVk1Cwl+qNNAdxVIEbfdq6YLoezsSTe17FiV8=; b=sLrlMd51vA+xtEFStCpoKrr9Uo219rmLraI0Evt/gezZENjP/iK3byC+0T7VsYXcw5 DRr/L/T161XVL1X+5+OLhmeyBb0GK50afEBoq9auS3kbmp6UFLxt88Gw7m5ckHEhi+pa zn0Sn7hhm09lxRDyswN2F2dfm92feV9/NC/2fV+77KuFEhT/m2vCz8e91e/6sNHLCE25 TW2oyVlnSBArIZPHyqrabRzX+uhamJwmqLd+wVxowGiZMVQ9TVk86DDmP2N3z5TpQqS0 mnBIZAcFF4sxCbk7GIPFpiY5yHusuTpAZQLlNtoTkXeSS6BGiABt58cW2zd6vkLSs9n1 vi1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=nEh/b2LVk1Cwl+qNNAdxVIEbfdq6YLoezsSTe17FiV8=; b=DZzbtkqYbfzoQLpXURJRmxtCOvGodRaj4HP5n6OOzoFiYK78U6+Wxe0CrztmaWGptH UXpWtZqPuTQHU6hMInwxKGb/hsXonkAMxe/PXZX+h7FbAYA0UOeh5JuZmbmAu7TAbCF6 se6svH25S2VKC4WzHI4Us50fhdkR/arso/b3qIFO6Rjs/QVtaLYnSrRyvNA260k7lV47 dJZwvDnSVwBkkiwLjaItxD7cq1qfydd695nJk8fcx5yo6y1qaui93g5DtDs79G7qY5G1 2onSY2VHywuuiKZXNC62sMFxNuUM6kKwzw+uh9qntzii8CHyAkvazplzRpzTZhTcjq8C oZlA== X-Gm-Message-State: AOAM531vf3hdXEal2vK4kkPQib0bjA7N2xYu2XI0eGCxniw+RFab2azL RlNzBjgf0KT7mvEndvgdMHGRNLN5p/bLAOtXEec= X-Google-Smtp-Source: ABdhPJwTuXdGoWhpejxHfDNOkEtjTqidomqwAMOJ0D0RYyxsIR3PT9zSZ1fBiK0te19/yZ0QNsRLwBi/60zuWAKAhPQ= X-Received: by 2002:ac2:58d3:: with SMTP id u19mr2103478lfo.159.1612367415666; Wed, 03 Feb 2021 07:50:15 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 3 Feb 2021 16:49:59 +0100 Message-ID: To: Levi Morrison Cc: internals Content-Type: multipart/alternative; boundary="0000000000000eeb4605ba708ba4" Subject: Re: [PHP-DEV] Proposal: Add ReverseArrayIterator and ForwardArrayIterator to SPL From: nikita.ppv@gmail.com (Nikita Popov) --0000000000000eeb4605ba708ba4 Content-Type: text/plain; charset="UTF-8" On Wed, Feb 3, 2021 at 4:38 PM Levi Morrison wrote: > Hello, everyone! > > This proposal adds two new classes to the SPL: > > - `Spl\ReverseArrayIterator`. It iterates over an array in reverse > order. It does not duplicate the array. > - `Spl\ForwardArrayIterator`. It iterates over an array in forward > (normal) order. It does not duplicate the array. > > They both implement Countable which returns the `count()` of the > array. The [PR][1] has some examples and discusses why I am proposing > `ForwardArrayIterator` when there is already `ArrayIterator`, the > short of which is for performance. There are timing numbers in [one of > the comments][2]. > > When it comes time to vote I may merge this into another RFC with > [`CachedIterable` by Tyson Andre][3], which I recommend readers also > take a look at. Whether we team up for the RFC vote or not, I wanted > to get this out there for discussion and review. > > [1]: https://github.com/php/php-src/pull/6535 > [2]: https://github.com/php/php-src/pull/6535#issuecomment-769179450 > [3]: https://github.com/php/php-src/pull/6655 > Hey Levi, I like the general idea of having an "ArrayIterator but sane". That said, I don't think that the ReverseArrayIterator + ForwardArrayIterator pair of iterators approaches this problem correctly. There are plenty of iterators that could be run in reverse, and I think it would be silly to create two classes for each of them. E.g. if we introduce an ObjectPropertyIterator, should there be both ForwardObjectPropertyIterator and ReverseObjectPropertyIterator? I don't think so. I think the correct abstraction for bidirectional iterators is to introduce an interface // Or "ReversibleIterator" interface BidrectionalIterator extends Iterator { public function prev(): void; public function end(): void; } and then a class along the lines of: class ReverseIterator implements BidirectionalIterator { public function __construct(private BidirectionalIterator $iter) {} public function next() { $this->iter->prev(); } // etc. } This would replace "new ReverseArrayIterator($array)" with "new ReverseIterator(new ArrayIterator($array))", but in a way that is general, and composes. Regards, Nikita --0000000000000eeb4605ba708ba4--