Re: [RFC] Generators

From: Date: Sun, 12 Aug 2012 22:33:23 +0000
Subject: Re: [RFC] Generators
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-62119@lists.php.net to get a copy of this message
On Sun, Aug 12, 2012 at 2:08 PM, Brian Moon <brian@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.

« previous php.internals (#62119) next »