Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62122 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42732 invoked from network); 13 Aug 2012 01:14:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Aug 2012 01:14:50 -0000 Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.42 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.215.42 mail-lpp01m010-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:57417] helo=mail-lpp01m010-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B0/20-00812-90558205 for ; Sun, 12 Aug 2012 21:14:50 -0400 Received: by lahl5 with SMTP id l5so1783194lah.29 for ; Sun, 12 Aug 2012 18:14:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=QNfy42RlScpdOgR/Z0HhUcefcMh6T8eJnEeKYVGwZQc=; b=zcnsUnX1PIpdfSXtLK87yQWJNrEdB6bc7pV7jU6pdyjxmoBfOMNhup8WBEQ86c/6Mo FUY1fy2U53qdZDJ3l1XDFAN+8oU78R++9R1Nn1xoSN74+V3AXfq+w2V48nPi3aJkP7hc Qau14ulif70ObmKHdWaXgPp3ezl4zEH6NPptr/+0JGw4tMDJ2Ip7GV6sTQB4ygcjn+nC tQo2flaoL0OZoLqrukhq7IwyUuvsMGxiR+d2TryBKf4LrUhjolqDdK7s1TLrrDUnQtm2 KcuOaXWfiD7fgeT7bOIjcdPrHj28GCY66HZoOxAUcxjra/dGTU1F2ZkYCo/t3WzAPLQA BkmQ== MIME-Version: 1.0 Received: by 10.112.43.135 with SMTP id w7mr5668670lbl.48.1344820486812; Sun, 12 Aug 2012 18:14:46 -0700 (PDT) Received: by 10.112.89.174 with HTTP; Sun, 12 Aug 2012 18:14:46 -0700 (PDT) In-Reply-To: <502842C1.1070401@moonspot.net> References: <50280D37.10109@moonspot.net> <502842C1.1070401@moonspot.net> Date: Sun, 12 Aug 2012 19:14:46 -0600 Message-ID: To: Brian Moon Cc: Nikita Popov , PHP internals Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] [RFC] Generators From: morrison.levi@gmail.com (Levi Morrison) On Sun, Aug 12, 2012 at 5:56 PM, Brian Moon wrote: > 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. No, Brian, it is 106 lines of actual code I would find in the wild. And sure, I'd have spaced the generator code more like: public function getIterator() { if ($this->left) { yield* $this->left; } if ($this->right) { yield* $this->right; } yield $this->value; } However, that's 11 lines of code compared to 106. > In the above example, what > sets $this->right? or $this->left? Iterators don't set $this->right and $this->left; they only traverse the tree. > I don't consider this a very good example. That's fine. You are entitled to your opinion. However, you didn't have to figure out the 106 lines of code that is the Post-Order iterator. That was a NIGHTMARE and I'm still not 100% confident that it works as it is supposed to. The generator, on the other hand, is simple and follows the definition very closely. MUCH better.