Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113112 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 70777 invoked from network); 8 Feb 2021 15:39:46 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Feb 2021 15:39:46 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9A2C0180506 for ; Mon, 8 Feb 2021 07:24:31 -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,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-io1-f43.google.com (mail-io1-f43.google.com [209.85.166.43]) (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 ; Mon, 8 Feb 2021 07:24:30 -0800 (PST) Received: by mail-io1-f43.google.com with SMTP id m17so4801617ioy.4 for ; Mon, 08 Feb 2021 07:24:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=datadoghq.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=DdXZDo9evTXrMl6oqcdCpS7dySriZnIpBqaKFaEjEtc=; b=NvLYS2paiq9b2Pko/Msjlb1rXkhWehIpJ2pvyafFMwW9zoQ0RR+ke1u7VJT3qLTASj xiJQvPQXvgeCeoXObLnJaMvHRb1G/9D7xmCZjRFq1cDgraIAZtBR264KNsfqoUR09Bee o07kQVawEotYdN4ebS9pEQ5dNSfAg304ZJxyY= 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=DdXZDo9evTXrMl6oqcdCpS7dySriZnIpBqaKFaEjEtc=; b=iINK43mVsZ2U7AgmCxPUCcZRV6R+pweYpBZb7PfhKteSoS//iZgFkEwFOFHZk9Og4v 6XiuG2n5ESGlslH3MlR8Ompxb92vTt3GZ+D5K0YN8YSDqScVE4ivq/l47eiMgspbWGNf 6t9CzB2FGfEUHeYTjAVITTMPwXkzYdQQ8MWgUAIWVFwJVSeoav8j27LJxOeoqduSboEB qThIG/OWCuyNvihvJLJ4zVHkuhCYHERk+GKZU+aGGs7WgwvEBAEa3Gp4AbCIPwl20Oaf cb1xN+zYmRu2jxSh/XYffHbgKc1Ac0TEUjB6XhK8kWcqzNKSPokJHptr4h10xXtMQGfT 4Dug== X-Gm-Message-State: AOAM532zDGNdrH/tMchyU+b9NDaSPvgua8euzqx/yHZPtq+fn3iT5VRZ 5zsTY9WU8zdk68ubkGcaiIMvW+TolTyUeq8pl/5Ksw== X-Google-Smtp-Source: ABdhPJyEpdZuVKIaXLaXFPQss5p75S/yFU5TtSEIA48ds2Wu0Cy36w/hw2ayIJjNctRzSgWyCTtASHzb32s1cwbscqk= X-Received: by 2002:a02:cb49:: with SMTP id k9mr18293718jap.58.1612797868504; Mon, 08 Feb 2021 07:24:28 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Reply-To: Levi Morrison Date: Mon, 8 Feb 2021 08:24:17 -0700 Message-ID: To: Nikita Popov Cc: Levi Morrison , internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Proposal: Add ReverseArrayIterator and ForwardArrayIterator to SPL From: internals@lists.php.net ("Levi Morrison via internals") 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 After thinking about it for a while, I think this will be fine. It doesn't encroach on the reversible IteratorAggregate space, which is the space that I didn't like from my previous proof-of-concept. I'd like it if `BidirectionalArrayIterator` was shorter, but plain `\Spl\ArrayIterator` is too likely to be confusing with `\ArrayIterator` and that's the only meaningfully shorter name I've thought of yet. This also plays into the bad ergonomics of the reversed array iterator: new ReverseIterator(new BidirectionalArrayIterator($array)) But as you say, the reversed version will not be common. I'll code this up soon unless someone objects. I'm very open to naming suggestions for a better performing, simpler array iterator.