Proposal: Adding a RewindableKeyValueIterator type allowing mixed/repeated keys
| From: | tyson andre | Date: | Sun, 31 Jan 2021 00:36:59 +0000 |
| Subject: | Proposal: Adding a RewindableKeyValueIterator type allowing mixed/repeated keys | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-113028@lists.php.net to get a copy of this message | ||
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