Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113029 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 68949 invoked from network); 31 Jan 2021 04:07:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Jan 2021 04:07:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E4F7D1804AA for ; Sat, 30 Jan 2021 19:49:39 -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-f46.google.com (mail-io1-f46.google.com [209.85.166.46]) (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 ; Sat, 30 Jan 2021 19:49:37 -0800 (PST) Received: by mail-io1-f46.google.com with SMTP id p72so13693418iod.12 for ; Sat, 30 Jan 2021 19:49:37 -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=jfqINY0YacWsXdyTU4bbkYLPjjk/CeN2nAuk0ocMTWA=; b=T8baz52zlZAEXbtx65LhgI5MaGehpov6UDHicrT9XFAso+mAcQxh4I2qyDCeTEMBXn h9fvqZf2v89HYViboPhX0y5FWN3ePVTsK6pZHxMn18B3Nob8EBLW8OY5/IrcGbC+CFdi lry6jZGAwx0gKIyid+kT2D12GstMWX9OAsga8= 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=jfqINY0YacWsXdyTU4bbkYLPjjk/CeN2nAuk0ocMTWA=; b=J4uDbHPegd2LeSxK/sOPYrQjb647XLGeyE9Q1VVVajUqmWybjVoxjFLUvuhFrm8rFL vLLCP6J5o0JtCHVLcEDPdJNWAYa8J5TtoD0HGVvDZJLELmP7q9Osh2DQlqxjsRgRBKio oatIcDe5K8ObnqKFeAickzVFhZZddgEBJo1zkfwkei5mJpNpc+gXkUtNL309gPl1qWbC dCZQC9bWVSLScEbjLbn4zLEHz4LRV7ACh9IIFeTdVnj6MB1PBuuNr5Ih2RgLYIaU58Cd p/2aS9J0EmGKXLtCvNTkNfjUhhPxdoQk1OComjV+X6JMYO4JK/RbPgsh652DUb3ref5/ Byxg== X-Gm-Message-State: AOAM531ijrP8BrtLUEMki+KPGIrDbtmu9A3DOMjoOD2+QP7NIGbFTgeq Y9ZppUneyn18TRkIeRbX64RT0iQ3lsXk1A6ZDC81pg== X-Google-Smtp-Source: ABdhPJwuwh++8FdhZNtzYZJN572Lsl08K1B98KU1UlBVbVc8jHRIAmcbpgDPNs1LuJrKMJjLp71yz/7YJC6b6vhpyhE= X-Received: by 2002:a02:634b:: with SMTP id j72mr10470664jac.106.1612064973823; Sat, 30 Jan 2021 19:49:33 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Reply-To: Levi Morrison Date: Sat, 30 Jan 2021 20:49:23 -0700 Message-ID: To: tyson andre Cc: "internals@lists.php.net" Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Proposal: Adding a RewindableKeyValueIterator type allowing mixed/repeated keys From: internals@lists.php.net ("Levi Morrison via internals") On Sat, Jan 30, 2021 at 5:37 PM tyson andre wrote: > > Hi internals, > > Currently, there don't seem to be any internal classes that can be used to store a copy of the keys and values of an arbitrary Traversable. > > - This would help in eagerly evaluating the result of a generator in a memory efficient way that could be exactly stored and reused > e.g. `return $this->cachedResults ??= new \RewindableKeyValueIterator($this->innerResultsGenerator());` > https://externals.io/message/108767#108797 > - This would be useful to exactly represent the keys of sequences with repeated keys (e.g. `yield 'first'; yield 'second';` implicitly uses the key `0` twice.) > - This would be convenient to have to differentiate between 1, '1', and true. > - This would be useful if php were to add internal global functions that act on iterables and return Traversables with potentially repeated keys based on those iterables, > e.g. map(), filter(), take(), flip(), etc > - If PHP were to add more iterable methods, being able to save an immutable copy of a traversable's result would be useful for end users. > - If PHP were to add a Map (ordered hash map with null/any scalar/arrays/objects as keys) class type in the future, > and that implemented IteratorAggregate, the return type of getIterator() would need something like RewindableKeyValueIterator. > - The lack of a relevant datatype is among the reasons why classes such as SplObjectStorage are still an Iterator instead of an IteratorAggregate. (and backwards compatibility) > > ``` > final class KeyValueSequenceIterator implements Iterator { > // loop over all values in $values and store a copy, converting > // references in top-level array values to non-references > public function __construct(iterable $values) {...} > public static function fromKeyValuePairs(iterable $entries): self {...} // fromKeyValuePairs([[$key1, $value1]]) > public function rewind(): void {...} > public function next(): void {...} > public function current(): mixed {...} > public function key(): mixed {...} > public function valid(): bool {...} > // and __debugInfo, __clone(), etc. > } > ``` > > Thanks, > - Tyson > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > The names `RewindableKeyValueIterator` and `KeyValueSequenceIterator` are just long-form descriptions of the Iterator API. I don't think we need such long names. The name should focus on what it brings. What it brings is a caching iterator around another iterator, that includes re-windability. The SPL provides a `CachingIterator`, but I assume it is inadequate somehow (it is basically SPL tradition). Can you specifically discuss the shortcomings of `CachingIterator` and how you will address them? To that end, have you implemented a proof-of-concept for the proposed iterator?