Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113051 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 10380 invoked from network); 2 Feb 2021 15:16:13 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Feb 2021 15:16:13 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D59661804E3 for ; Tue, 2 Feb 2021 06:59:27 -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=-1.9 required=5.0 tests=BAYES_00,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 NAM02-CY1-obe.outbound.protection.outlook.com (mail-oln040092004015.outbound.protection.outlook.com [40.92.4.15]) (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 ; Tue, 2 Feb 2021 06:59:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f/uuoYoVyprAQcMlFh0lWbLfWL5dpt0ArW3X4cTdTRzNVl1C9QCQwtwTamJfAUI/LI8LxdHTZjFPAPSKWt7eDr76yl2G3UifVA2SJxBWoOKG2HKNDsY8JTlZ/4eP3ZKaniFnSpxlTNJ1f293k+MhGBOhwgjxAEblNN7u+hnOwxumG0jBV7EyhKf4mMfy7WGlcHRXtJ284EcjGidjdFtLuVkepIz4skav8QJ32pDMef65Knb0Y1ROHVVBXl9oM7tIv6yexzFi9MU2FGJu+Q+V0c/fObiUu76SbpNqy/gdG9mck7jOCOd/D6x+xyH5f2TuHofaQMF3bKfp/AFdc6M1RA== 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=YQD7o526V4Ejst+A1vm8ubAF4YiMp48zOM/XoDx5r/o=; b=D6xpvMDikCWJodPYQ9Zoy1Plic013e21evYqhbn7pDqUNvpIsAGWwg69kAh3nNkLunsDsRDT3pFIb1oxrdmmEuBZZJ32bHx0nXn70e4tJ/iFgcRzfminthFcL0+0blDXlcJfQfX30rBrf6UHrDI3ELouY8ZicjYqHO3PBljW0HOvWifjMxbG7gaE6jJriOf0n0bi9gUMp1fnx0gRhUN0VyIhZEGL8zYmqXxo7+gq7LL1p8s/EtOdBTHMrk0gtRxil/3PcPWct86TA0UI9Y11Ne90F9HRoVQkKLCR9asIpbTsTJlNIyHFN2j6jO7spBNNd184UsnMkO8dV+T1y3XaCw== 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=YQD7o526V4Ejst+A1vm8ubAF4YiMp48zOM/XoDx5r/o=; b=S3k6Lz30flIxeLrMZauKML0kPbL5d7QEU0vfMJ9fuKIk+gT7VLbBgAo6gBQRhx8bj0NV4wAhNRux4v11Ncq4hZ+O8xSWhpXmEW9KeqiKpFTp88aAedsNKXOU65CznAcmBPM5y9/PwxS18Yr9vJodqHCpFu+OndlEAD3WqnKro/ZmmM5qxBMYI1baUIrJJch0yz+kfNdpG8XclwOwuo/+Ey8GCfqhoQlme+8VtsgojaOdYLCHN8CzNiwyAo5bIJ4qpSq/rTF6C+LSfRYdQRBp7V0UgTCS2MJVwMqZV14FsRXTpJ+oQe7uBGtwfo5Gm4YbzgY8HP9dtNec8tHuTFg1Pg== Received: from CY1NAM02FT060.eop-nam02.prod.protection.outlook.com (10.152.74.52) by CY1NAM02HT165.eop-nam02.prod.protection.outlook.com (10.152.75.154) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.12; Tue, 2 Feb 2021 14:59:25 +0000 Received: from DM6PR07MB6618.namprd07.prod.outlook.com (2a01:111:e400:7e45::51) by CY1NAM02FT060.mail.protection.outlook.com (2a01:111:e400:7e45::252) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3784.12 via Frontend Transport; Tue, 2 Feb 2021 14:59:25 +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.028; Tue, 2 Feb 2021 14:59:25 +0000 To: "internals@lists.php.net" Thread-Topic: Proposal: Adding a RewindableKeyValueIterator type allowing mixed/repeated keys Thread-Index: AQHW92K4Q7dSQ8CfzUK9YysQDgaLrqpE983L Date: Tue, 2 Feb 2021 14:59:25 +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:E05C3E609687EEEB3EA95D00FFCC5CA68315D7F6367722764C098D2539A39F85;UpperCasedChecksum:E75E605A9B2FD14FF56C047D70BC7782FA331F489E43C1938A1629455984C3DF;SizeAsReceived:7123;Count:44 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [LoJfLxVX/0zsMJzPyphlsZ10N1iZC6c/g9BPsS+FEg/mtcuaSx4vHuVOApD8fkhJ] x-ms-publictraffictype: Email x-incomingheadercount: 44 x-eopattributedmessage: 0 x-ms-office365-filtering-correlation-id: 0e797d36-a2ee-4f23-8364-08d8c78b21e3 x-ms-traffictypediagnostic: CY1NAM02HT165: x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: bd8aBlSpxNe3/z7jB2R/AAIpp6UUVVfsO3SXSnB5Hy/FuH9Z4aspggZp591XnJN1sGQBdalEPfeifdQ+DoHepMouvEJEJ3y/CqGvyyVq3tg2Bq3Rin6yEtrF+KjpwjTk+CYQbskFYT6lzHKcyZvONzF0zvZo+wlZO/Es9Jyqv6fuBnvb0hxZbZbTk1BDpGft5S+iXKTI6kowL0bp3HsCOAPs94DijL3nlBfRlSoPwQmeJeiqAtuO5m0PidMUfTEBneg7+cu29kG//qfjD2zFM+tSe8PlCl50ML3dgdw8/pV8OduQSYBM/gcmyv73HuVpSlIzn9DWipm5c76Kccp6fUOU1Qu1scYLNig9g+BpyqYjKSHtTmhG1RM5H4M28+VVWFL4LvOcRY8MNaOudN8MJ4VFIwl7T/livTJFlFxuWE0= x-ms-exchange-antispam-messagedata: mZ/2Bds0qW3o2qarRQRMnJ0qmQdNHnazUZeJ2YulwxQwhQJ6l5CXJQaDi30j7pyo51Zm45XoCRKbJpUkNERcm6w2PGJLeApaa9QrCMDvfNGDQVMfuiB96xjwcNbFabH4brQEtaX3QJM07Apw+mIpOzK70A+d69BisC8fj+c3TBAfO7EsZdgQKi3VTbk27jpOmmncWfxGR/e9WTi/6OGl3A== 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: CY1NAM02FT060.eop-nam02.prod.protection.outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 0e797d36-a2ee-4f23-8364-08d8c78b21e3 X-MS-Exchange-CrossTenant-originalarrivaltime: 02 Feb 2021 14:59:25.5015 (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: CY1NAM02HT165 Subject: Re: Proposal: Adding a RewindableKeyValueIterator type allowing mixed/repeated keys From: tysonandre775@hotmail.com (tyson andre) Hi internals,=0A= =0A= > Currently, there don't seem to be any internal classes that can be used t= o store a copy of the keys and values of an arbitrary Traversable.=0A= > =0A= > -=A0This would help in eagerly evaluating the result of a generator in a = memory efficient way that could be exactly stored and reused =0A= > =A0 e.g. `return $this->cachedResults ??=3D new \RewindableKeyValueIterat= or($this->innerResultsGenerator());`=0A= > =A0 https://externals.io/message/108767#108797=0A= > - This would be useful to exactly represent the keys of sequences with re= peated keys (e.g. `yield 'first'; yield 'second';` implicitly uses the key = `0` twice.)=0A= > - This would be convenient to have to differentiate between 1, '1', and t= rue.=0A= > - This would be useful if php were to add internal global functions that = act on iterables and return Traversables with potentially repeated keys bas= ed on those iterables,=0A= > =A0=A0 e.g. map(), filter(), take(), flip(), etc=0A= > - If PHP were to add more iterable methods, being able to save an immutab= le 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/arrays/= objects as keys) class type in the future,=0A= > =A0 and that implemented IteratorAggregate, the return type of getIterato= r() would need something like RewindableKeyValueIterator.=0A= > - The lack of a relevant datatype is among the reasons why classes such a= s SplObjectStorage are still an Iterator instead of an IteratorAggregate. (= and backwards compatibility)=0A= > =0A= > ```=0A= > final class KeyValueSequenceIterator implements Iterator {=0A= > =A0=A0=A0 // loop over all values in $values and store a copy, converting= =0A= > =A0=A0=A0 // references in top-level array values to non-references=0A= > =A0=A0=A0 public function __construct(iterable $values) {...}=0A= > =A0=A0=A0 public static function fromKeyValuePairs(iterable $entries): se= lf {...} // fromKeyValuePairs([[$key1, $value1]])=0A= > =A0=A0=A0 public function rewind(): void {...}=0A= > =A0=A0=A0 public function next(): void {...}=0A= > =A0=A0=A0 public function current(): mixed {...}=0A= > =A0=A0=A0 public function key(): mixed {...}=0A= > =A0=A0=A0 public function valid(): bool {...}=0A= > =A0=A0=A0 // and __debugInfo, __clone(), etc.=0A= > }=0A= > ```=0A= =0A= This has been updated - the preferred name is CachedIterable.=0A= It turns out InternalIterator is generally more efficient, =0A= and SplFixedArray is already using that.=0A= The implementation is pretty much done - https://github.com/php/php-src/pul= l/6655=0A= =0A= ```=0A= /**=0A= * Internal class that can be used to eagerly evaluate and store a copy=0A= * of the keys and values of an arbitrary Traversable.=0A= * Keys can be any type, and keys can be repeated.=0A= */=0A= final class CachedIterable implements IteratorAggregate, Countable=0A= {=0A= public function __construct(iterable $iterator) {}=0A= public function getIterator(): InternalIterator {}=0A= public function count(): int {}=0A= public static function fromPairs(array $pairs): CachedIterable;=0A= =0A= public function __serialize(): array {} // [$k1, $v1, $k2, $v2,...]=0A= public function __unserialize(array $data): void {}=0A= =0A= // useful for converting iterables back to values=0A= public function keys(): array {}=0A= public function values(): array {}=0A= // useful to efficiently get offsets at the middle/end of a long iterab= le.=0A= public function keyAt(int $offset): mixed {}=0A= public function valueAt(int $offset): mixed {}=0A= // dynamic properties are forbidden=0A= }=0A= ```=0A= =0A= Any other comments?=0A= =0A= Thanks,=0A= - Tyson=0A=