Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113066 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 43164 invoked from network); 3 Feb 2021 16:45:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Feb 2021 16:45:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E204518053D for ; Wed, 3 Feb 2021 08:29:15 -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-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) (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 08:29:15 -0800 (PST) Received: by mail-lj1-f176.google.com with SMTP id t8so28944783ljk.10 for ; Wed, 03 Feb 2021 08:29:15 -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=fqYqdkWZWSMSEZnC+o94l38CRN4pn3PUhrjM/6OxdW4=; b=ZIiDOWtdQEdNvudkQiaIyVEnHD1GYgShuObTV9XDnQ1tsOXt/afZthPR984jcwUb17 nQyKu5SCfwQy+OAiYVejopIkWS2A2745HCnq2y1nAfnm5NULkMA83EvzoHCCVZGtnEQ1 MHTKSrsGl31Iwh2hb6NqHP2j6KYybphOrNHl0KaLu/1vZkjzVghxbSmuiMvqd+xM37mQ r9ujwmtQ6HA3dQu/qMCkSB63O+Gzj7FRN69DWRG5xdJUG2XYn8OelMJ0DWvdR74Ev3Za 9Qt6MXjUMp//sXKfnKfNl2+7DQF5Mh5lqyeLu6KeCbXm2NMhIJvFJQhozJRd58yWreYV r7tg== 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=fqYqdkWZWSMSEZnC+o94l38CRN4pn3PUhrjM/6OxdW4=; b=FGbaor6av1FKlQ/xZNtwYM+/9/ikXrNYp0wYjpaNLPZV9NlnWSPSTqSppr6s60wmNd JXJOlb/7Sk8v51uS/l5KrKKWAaVQEzUVyfT7CxTCvBVdfBNfFdiCkgnc1Wo1HXYwCXZv CuG6K6hCCj/KcMWOF7TA+3Qn5mRawxB2Rt5sDVWCsROcUdjK/peHDoOKTqO1IoeLJDd4 P77zDT02b/yUlogsCFwc7qxAucPtZ8uUoUAWjDV8/vHfe3YPieerYYnGvcpnED0CkEv/ l1DyFRRoBHKEO9lTYQUOn4Ace9t24VjqUV16mlFR+FD96JhcBT4UWycRzPKn39Fzpz0Y /qEg== X-Gm-Message-State: AOAM533eH5193sULTGSQj3msgmfJ2sTXddEumvOT93ymBPvaKE/Yaw7Z tT31fux3NknU+Q0WzLUMYOi95fBtPAWfRFKhyr8= X-Google-Smtp-Source: ABdhPJwMlDTsgt1yKAbFOAZnc4GIbBHucy+4iVmUH9GKoMbFnTTh1ENlgCJT/tgWb2+IpE4x9R8AIS6jWcwq3dTEQOo= X-Received: by 2002:a2e:88c9:: with SMTP id a9mr2274603ljk.29.1612369750751; Wed, 03 Feb 2021 08:29:10 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 3 Feb 2021 17:28:54 +0100 Message-ID: To: Levi Morrison Cc: internals Content-Type: multipart/alternative; boundary="0000000000003d7e9205ba71161f" Subject: Re: [PHP-DEV] Proposal: Add ReverseArrayIterator and ForwardArrayIterator to SPL From: nikita.ppv@gmail.com (Nikita Popov) --0000000000003d7e9205ba71161f Content-Type: text/plain; charset="UTF-8" On Wed, Feb 3, 2021 at 5:09 PM Levi Morrison wrote: > On Wed, Feb 3, 2021 at 8:50 AM Nikita Popov wrote: > > > > 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 > > I wrote an implementation of this idea two years ago: > > https://github.com/php/php-src/compare/master...morrisonlevi:BidirectionalArrayIterator > > It's fine for arrays, but not all structures that you can iterate > through forward or reverse. For instance, tree structures can be > traversed forward or reverse, but bidirectionally is far more > difficult (and I did not find any pre-existing solutions in this > space, btw). I do have repositories with tree structures, so it's not > just theoretical. > I don't really view this as a problem, in the sense that you always have the option of implementing two iterators. But for most cases, if you can iterate in reverse, you can also iterate bi-directionally. > Another factor for abandonment was ergonomics. This is not great: `new > ReverseIterator(new BidirectionalArrayIterator($array))`. It's mildly > less performant as well, having to create two objects, and then having > delegation through methods. > It is slightly less ergonomic in isolation, but I think it makes more sense conceptually. Iterating an array in reverse is not exactly common (to the point that I'm not even sure this is a problem worth solving), so I don't think it needs to be optimized for minimum characters. Regards, Nikita --0000000000003d7e9205ba71161f--