On 13 באוג 2012, at 01:33, Levi Morrison <morrison.l...@gmail.com> wrote:
> 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 > Each Iterator implementation in PHP is longer and require a class. The yield keyword can reduce the code and make things simpler. If my vote count, though I can't believe it is, I'd vote for it - clean syntax, make things simpler to develop after you understand the concept (for those who against adding because of the learning issue and beginners - we'll have beginners anytime that'll mess up with the concept, so I don't think that it should restrict us. Put that aside, we should make the documentation clear and easy as possible in order to help beginners understand the concept). -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php