Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113031 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 1656 invoked from network); 31 Jan 2021 16:01:05 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 31 Jan 2021 16:01:05 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2CC9A1804F6 for ; Sun, 31 Jan 2021 07:43:50 -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=0.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10olkn2089.outbound.protection.outlook.com [40.92.40.89]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 31 Jan 2021 07:43:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ePqaOuQ7x/F1qC39iu1I7cG/d5j+Pyz3m+lK43vbjnaRXgzZvJzjEwT3x4IJLvLqf+Gx07WBy+bYKnI0rAWOdtCIEbkdArJCJbSK+WdWgagerE7SCGQgNX7fDcaHIqhrP5dUFsqwuHLdj0VlTPEV6/1d1Ycl+fGN7ZLvJTd2vNZZOQ4VJVX2XaTaQ3TfpwHgildAys8FC8wYyyvNfQHgcaiPjCOFlaSafxc+BVX9ufpQ7dJTZkG7EBkEWqQzFTHL2gdHatJboZWvk5vMCDjlc91IaNgCvdK3ILXmDFFQQLrqi9wC4N39/uremz3HRizBbKyrN5ut4snfQye35qt0vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0YHY+YISqTYdUm/TP+l0nCbH/YtR24t5TrUbZ3a+PFk=; b=Kg9q4I4wW19Q7A6lRLQP4L7fLweLodzOrLpEBW/8hRSVX/Earbe9pK1v7FGWTS+VPIM/AXlHhB4PPTVerT1v+meLf4kbi/n+92Q6TdPeFr8AYHDYJ6XREivBKTE3KBVn+kKPGtHQRwMQgiNjaTRs6papg1sG2+lOjjl4TYizWsq8lzG9NF/xlIA34EVKdbnkdG6bhmVf4aNCSdRvISc6CLdHFZT2erIn2wRRpqrU6A1uodI/8v9DQMaiykCzm2cim4WufLI9OuYA+2ABoi6yJ/AYhZLutkbloIOttiGzAbAOFmFtIkTi1U8rmP33po2lpJeQVvfSMIGja5RwKb6v5w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hotmail.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=0YHY+YISqTYdUm/TP+l0nCbH/YtR24t5TrUbZ3a+PFk=; b=nManqOSC8I2A6nzfEUM8ZOseZfcF03yquwl71AHkYl0uNVOfAdXAN03MrUdjhmTpcvnx0TKXCxyjkqNuB4ou+Td0Gu69U3Hvx4ds1WdR90bNU0WA7tRtq8kOKVJrlEQ3nMpUG2j7jRfKPwCmRP7whM8LJfEqC58U8wpvvLpwQgt4nj4N+NhxRHEIXbbaPHc5vPjcqq1Re2R03WPnNDPa7W3INk9OMRilQ0Dg5TNs26Rb7Tvm0MyWoc0QdSdyEGfHgoM2dT28Fls2J9ltd6raytOJfspOGli+zgm0jBLr6rNcXadCN7CSlJwWgf+1hcz8/nDfbbSWp1BS9PUl6P/b9g== Received: from DM6NAM10FT005.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e86::46) by DM6NAM10HT012.eop-nam10.prod.protection.outlook.com (2a01:111:e400:7e86::363) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.11; Sun, 31 Jan 2021 15:43:48 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com (2a01:111:e400:7e86::41) by DM6NAM10FT005.mail.protection.outlook.com (2a01:111:e400:7e86::87) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.11 via Frontend Transport; Sun, 31 Jan 2021 15:43:48 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::b4c4:dc11:5337:821d]) by DM6PR07MB6618.namprd07.prod.outlook.com ([fe80::b4c4:dc11:5337:821d%4]) with mapi id 15.20.3805.024; Sun, 31 Jan 2021 15:43:48 +0000 To: Levi Morrison CC: "internals@lists.php.net" Thread-Topic: [PHP-DEV] Proposal: Adding a RewindableKeyValueIterator type allowing mixed/repeated keys Thread-Index: AQHW92K4Q7dSQ8CfzUK9YysQDgaLrqpBGVqAgADE/o8= Date: Sun, 31 Jan 2021 15:43:48 +0000 Message-ID: References: , In-Reply-To: Accept-Language: en-CA, en-US Content-Language: en-CA X-MS-Has-Attach: X-MS-TNEF-Correlator: x-incomingtopheadermarker: OriginalChecksum:95166325FC7708E63EFE81B0DC86A177957D1216F682B122471295A4BE1EFEE6;UpperCasedChecksum:FAC1D9A6735C2EE751FA45DFF74B956E6A9A24FC0B42AB20B9DC04DCB78D71D0;SizeAsReceived:7259;Count:45 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [GXW+N35LBkfInjiVaqDkDv8TxiG7cr4zPMjm9PneM67s3p4LhHZaMGa0HUBxa+Hr] x-ms-publictraffictype: Email x-incomingheadercount: 45 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: a815c418-3d31-4e5d-550b-08d8c5ff0017 x-ms-traffictypediagnostic: DM6NAM10HT012: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: PSIkVGHU2jjVcskTvSk+PMp68UoPwtfhCri8t3ch9EQ2y1yosmHJrWMPO2A7yFNwXoouWS4dVF4pJE8EG5g0ak91YeuMmAMMzWyj7dX1gIuFnRe/hgwRjZyLvES+IXfkaUzumtsFI9qlRKk3LKN4/ZCTKtExay1G2IxRO52RWQ7PoEuA2ZKytYVIdji6IhHYjx/fv+cLsAdfljWBsVTikKjb8IBpexRuTufj+qnwLdPjSQMLG8ZTKrpC8FYxyU7EelpCFCMZpFd1EhWd3h+TCL9SnMuxLlvgBFILphmbXmOV07fSVas6AIf6LnUjJC11BB5iaQBS+pczgfveZC5UgVuT3SlZPNApMQnMLMAShikeAQB1MR480ZJUzeYffbdizDWb0eZYn6kC5+RDYxg5mPAH9J93Zxi8dm7mrIBEJ1Q= x-ms-exchange-antispam-messagedata: /P0mwIMsDFxyth6UATtds0K1bUohFigii4tmrVVQAk3BIcq6awtwkYDPP4P4ADLlyUid+uTDqyxU0qTpWhvavmcEQ7wiu1PqCtuwAEHHHqie7lZQ4yZnY+wQkH3+HESBbEwhYoVRBr4qTQtwJ2WxkGrosxfWRMeAh9Fyh0+Clb65VaDU1WaSizQoR33qCI4aMV19c3o9XlINxz0L4eRjqw== x-ms-exchange-transport-forked: True Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: hotmail.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-AuthSource: DM6NAM10FT005.eop-nam10.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: a815c418-3d31-4e5d-550b-08d8c5ff0017 X-MS-Exchange-CrossTenant-originalarrivaltime: 31 Jan 2021 15:43:48.1306 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6NAM10HT012 Subject: Re: [PHP-DEV] Proposal: Adding a RewindableKeyValueIterator type allowing mixed/repeated keys From: tysonandre775@hotmail.com (tyson andre) Hi Levi Morrison,=0A= =0A= > > 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.=0A= > >=0A= > > - This would help in eagerly evaluating the result of a generator in a = memory efficient way that could be exactly stored and reused=0A= > >=A0=A0 e.g. `return $this->cachedResults ??=3D new \RewindableKeyValueIt= erator($this->innerResultsGenerator());`=0A= > >=A0=A0 https://externals.io/message/108767#108797=0A= > > - This would be useful to exactly represent the keys of sequences with = repeated keys (e.g. `yield 'first'; yield 'second';` implicitly uses the ke= y `0` twice.)=0A= > > - This would be convenient to have to differentiate between 1, '1', and= true.=0A= > > - This would be useful if php were to add internal global functions tha= t act on iterables and return Traversables with potentially repeated keys b= ased on those iterables,=0A= > >=A0=A0=A0 e.g. map(), filter(), take(), flip(), etc=0A= > > - If PHP were to add more iterable methods, being able to save an immut= able copy of a traversable's result would be useful for end users.=0A= > > - If PHP were to add a Map (ordered hash map with null/any scalar/array= s/objects as keys) class type in the future,=0A= > >=A0=A0 and that implemented IteratorAggregate, the return type of getIte= rator() would need something like RewindableKeyValueIterator.=0A= > > - 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)=0A= > >=0A= > > ```=0A= > > final class KeyValueSequenceIterator implements Iterator {=0A= > >=A0=A0=A0=A0 // loop over all values in $values and store a copy, conver= ting=0A= > >=A0=A0=A0=A0 // references in top-level array values to non-references= =0A= > >=A0=A0=A0=A0 public function __construct(iterable $values) {...}=0A= > >=A0=A0=A0=A0 public static function fromKeyValuePairs(iterable $entries)= : self {...} // fromKeyValuePairs([[$key1, $value1]])=0A= > >=A0=A0=A0=A0 public function rewind(): void {...}=0A= > >=A0=A0=A0=A0 public function next(): void {...}=0A= > >=A0=A0=A0=A0 public function current(): mixed {...}=0A= > >=A0=A0=A0=A0 public function key(): mixed {...}=0A= > >=A0=A0=A0=A0 public function valid(): bool {...}=0A= > >=A0=A0=A0=A0 // and __debugInfo, __clone(), etc.=0A= > > }=0A= > > ```=0A= >=0A= > The names `RewindableKeyValueIterator` and `KeyValueSequenceIterator`=0A= > are just long-form descriptions of the Iterator API. I don't think we=0A= > need such long names. The name should focus on what it brings.=0A= >=0A= > What it brings is a caching iterator around another iterator, that=0A= > includes re-windability. The SPL provides a `CachingIterator`, but I=0A= > assume it is inadequate somehow (it is basically SPL tradition). Can=0A= > you specifically discuss the shortcomings of `CachingIterator` and how=0A= > you will address them? To that end, have you implemented a=0A= > proof-of-concept for the proposed iterator?=0A= =0A= It caches the results by coercing keys and inserting them into an array, wh= ich is a different use case.=0A= If you rewind a CachingIterator, it rewinds the iterator that it wraps,=0A= which throws for Generators and other types of iterators.=0A= =0A= For RewindableKeyValueIterator, it needs to store keys that can't be used a= s array keys.=0A= =0A= I was planning to implement a proof of concept if there wasn't widespread o= pposition=0A= and if nobody pointed out the functionality already existed.=0A= =0A= ```=0A= $value) {=0A= printf("Key: %s\nValue: %s\n", json_encode($key), json_encode($valu= e));=0A= }=0A= }=0A= function yields_values(): Generator { yield 0 =3D> 'first'; yield '0' =3D> = 'second'; }=0A= $c =3D new CachingIterator(yields_values(), CachingIterator::FULL_CACHE);= =0A= echo "First CachingIterator iteration\n";=0A= dump_iterable($c);=0A= var_export($c->getCache()); echo "\n"; // array(0 =3D> 'second') does not r= epresent that data=0A= echo "Second CachingIterator iteration\n";=0A= // Fatal error: Uncaught Exception: Cannot rewind a generator that was alre= ady run=0A= dump_iterable($c);=0A= ```=0A= =0A= Thanks,=0A= - Tyson=