> 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.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to