Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62119 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 33538 invoked from network); 12 Aug 2012 22:33:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Aug 2012 22:33:39 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; 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:49812] helo=mail-lpp01m010-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DF/CE-00812-83F28205 for ; Sun, 12 Aug 2012 18:33:29 -0400 Received: by lahl5 with SMTP id l5so1749101lah.29 for ; Sun, 12 Aug 2012 15:33:23 -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=9QumjbWW//aBIa1sv294csMjWb1KboTqYgSX/ueAxO8=; b=lP3lTIgkgrvSl2MjybJWfsHQAHYHDiwpr0azLpiHONy1vcVJBmVv/urhwiHEpfc8dx Xiuspdrm/b40wA9ZZ3nConzxvBFnAe+29nrzvRoPgQkFJy9Uny4sVHzKExXbPwPOYBj/ hCoPkjvarWwpYk8nxvjjDQVGtX4h9MVASeu6aD+zYu/TRF2sbQY3tUxvb2cf6MOQrRgX dlU8LqGnyrztO0WlG6oNUJOPk2q90z6KW5o9/l03zI5HdYiCztNr4F6XLE6/BwwdQQjI Q+el2dmKzK8c3IkHbIb8p3qJ9OKfdfs6uvCo9GZ+ExJDhaPqjHQjziP4GRe1jlQPGvbJ 7WnA== MIME-Version: 1.0 Received: by 10.112.88.106 with SMTP id bf10mr5469277lbb.43.1344810803628; Sun, 12 Aug 2012 15:33:23 -0700 (PDT) Received: by 10.112.89.174 with HTTP; Sun, 12 Aug 2012 15:33:23 -0700 (PDT) In-Reply-To: <50280D37.10109@moonspot.net> References: <50280D37.10109@moonspot.net> Date: Sun, 12 Aug 2012 16:33:23 -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 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.