Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62121 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 39123 invoked from network); 12 Aug 2012 23:56:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Aug 2012 23:56:58 -0000 Authentication-Results: pb1.pair.com header.from=brian@moonspot.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=brian@moonspot.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain moonspot.net designates 72.5.90.26 as permitted sender) X-PHP-List-Original-Sender: brian@moonspot.net X-Host-Fingerprint: 72.5.90.26 mail.dealnews.com Linux 2.6 Received: from [72.5.90.26] ([72.5.90.26:35327] helo=mail.dealnews.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CC/9F-00812-9C248205 for ; Sun, 12 Aug 2012 19:56:58 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.dealnews.com (Postfix) with ESMTP id 317AE617B27B; Sun, 12 Aug 2012 19:56:54 -0400 (EDT) X-Virus-Scanned: amavisd-new at dealnews.com Received: from mail.dealnews.com ([127.0.0.1]) by localhost (atl-zimbra1.dealnews.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yA5rUEhjB-yN; Sun, 12 Aug 2012 19:56:49 -0400 (EDT) Received: from Brians-MacBook-Pro.local (h105.248.18.98.static.ip.windstream.net [98.18.248.105]) by mail.dealnews.com (Postfix) with ESMTPSA id 18490617B19D; Sun, 12 Aug 2012 19:56:49 -0400 (EDT) Message-ID: <502842C1.1070401@moonspot.net> Date: Sun, 12 Aug 2012 18:56:49 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: Levi Morrison CC: Nikita Popov , PHP internals References: <50280D37.10109@moonspot.net> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Generators From: brian@moonspot.net (Brian Moon) On 8/12/12 5:33 PM, Levi Morrison wrote: > On Sun, Aug 12, 2012 at 2:08 PM, Brian Moon 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. Well, it's 52 lines, not 106. The 5 lines above are not commented nor are they spaced at all like the ones in the class. In the above example, what sets $this->right? or $this->left? There has to be more calling code around this. I don't consider this a very good example. Brian. brianlmoon@php.net