Re: RFC Draft: Comprehensions
| From: | Robert Hickman | Date: | Thu, 21 Mar 2019 15:21:35 +0000 |
| Subject: | Re: RFC Draft: Comprehensions | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-104856@lists.php.net to get a copy of this message | ||
> But $filteredArrayIterator->next() is actually $arrayIterator->next() with
> a built-in if check, so you could also picture it as doing this:
>
> while ( $element = $arrayIterator->nextIf ( someCondition ) ) {
> $newArray[] = $element;
> }
>
In this case nextIf() would have to be implemented something like:
function nextif($someCondition) {
foreach($this->iteratorValue as $x) {
if(<some comparison of $x and $$someCondition) {
yield $x;
}
}
}
iterator_to_array would need an internal loop to pull the values from
the generator. Thus, as far as I can see, the generator implementation
would result in two loops running in lock step, rather than only one
in the eager case.
You do make a good point about the potential for the optimiser to
recognise and automatically deal with such cases however.