Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62120 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 35636 invoked from network); 12 Aug 2012 22:55:35 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Aug 2012 22:55:35 -0000 Authentication-Results: pb1.pair.com header.from=g.b.yahav@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=g.b.yahav@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.176 as permitted sender) X-PHP-List-Original-Sender: g.b.yahav@gmail.com X-Host-Fingerprint: 209.85.212.176 mail-wi0-f176.google.com Received: from [209.85.212.176] ([209.85.212.176:35103] helo=mail-wi0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B9/2F-00812-56438205 for ; Sun, 12 Aug 2012 18:55:34 -0400 Received: by wibhn17 with SMTP id hn17so2419911wib.11 for ; Sun, 12 Aug 2012 15:55:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=2G+O22x1wsoeNTPiQOQQ0kTZ0fvRCpFBRdIVHoha4Es=; b=s5hUPQA4AlAyIW1mTdC+I/Ugh6a6AWjhTlrUnC9nZIFEP2bfXb6YYWUG1JDxmXn1EP 4jAW1kYCFDe6y7VF0M7neQ17nD+XHP2Yc63c/rO4xSk4niqo1paidTWyFcZT/jfKiMot XDn7hp+i+kDXA7955aaVI5vwpaY1Enfj93HMcZFtOOltjs6qgff74T8XVtVnIRYOnYpe xlS1tGZ8YQeyR4bLOFvZPrJXMGt35EI1ISFwiH6A7oT+2teSTZcUu8BvHvPsY6fbt20J n0q85MPPWBsKEf4dEChyPSC7h5TMYc5ln5MRI0ozUpn6XuVx+9UFmxoxdnsidmiEFH2m 0Q3g== Received: by 10.180.89.235 with SMTP id br11mr13436219wib.3.1344812127314; Sun, 12 Aug 2012 15:55:27 -0700 (PDT) Received: from [10.0.0.7] (bzq-79-183-156-21.red.bezeqint.net. [79.183.156.21]) by mx.google.com with ESMTPS id fb20sm18944994wid.1.2012.08.12.15.55.25 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 12 Aug 2012 15:55:26 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 6.0 \(1485\)) In-Reply-To: Date: Mon, 13 Aug 2012 01:55:23 +0300 Cc: Brian Moon , Nikita Popov , PHP internals Content-Transfer-Encoding: quoted-printable Message-ID: References: <50280D37.10109@moonspot.net> To: Levi Morrison X-Mailer: Apple Mail (2.1485) Subject: Re: [PHP-DEV] [RFC] Generators From: g.b.yahav@gmail.com (Yahav Gindi Bar) On 13 =D7=91=D7=90=D7=95=D7=92 2012, at 01:33, 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? >=20 > 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/Pos= tOrderIterator.php. > An iterator using a generator looks something like: >=20 > public function getIterator() { > if ($this->left) yield* $this->left; > yield $this->value; > if ($this->right) yield* $this->right; > } >=20 > 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. >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >=20 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).