On Sun, Aug 12, 2012 at 2:08 PM, Brian Moon <br...@moonspot.net> wrote:
> Do you have a good example usage other than a file? I don't find 
> fopen/fgets/fclose all that complicated. What are the other valid use cases 
> for such a thing?

One fabulous use case is creating an iterator for a Binary Search
Tree.  A post order done without generators looks like:
https://github.com/morrisonlevi/PHP-Datastructures/blob/master/src/Spl/PostOrderIterator.php.
 An iterator using a generator looks something like:

    public function getIterator() {
        if ($this->left) yield* $this->left;
        yield $this->value;
        if ($this->right) yield* $this->right;
    }

This is 5 lines.  The fully commented version of the post-order
iterator previously mentioned is 106 lines of code and is considerably
harder to understand.

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

Reply via email to