Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62125 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 49404 invoked from network); 13 Aug 2012 02:39:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Aug 2012 02:39:53 -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.217.170 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.217.170 mail-lb0-f170.google.com Received: from [209.85.217.170] ([209.85.217.170:54395] helo=mail-lb0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2F/41-00812-8F868205 for ; Sun, 12 Aug 2012 22:39:52 -0400 Received: by lbbgp3 with SMTP id gp3so1858007lbb.29 for ; Sun, 12 Aug 2012 19:39:49 -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=klLHcT8os/aSAxgUJ3DKcUxuBErxv9vjJGoAxYMKXgA=; b=Mz/tLBGksZuGmB14nO5Uanqu0m5UO1X+uha3HCBQESbWwRoR/V2ifMz6cOZZGjsFuR oKn/i4cfW8jvdaq4GtU6VIJmDWbhju0dJvIyofC/LqbRwndzj6KUIkTbfO1FqnjemhD+ jaxwQR2TVqUXQ7LWupkybA10pzmF8ZGILvfZAFeJ+2z7qoBI1J5LbxAn1d2v9wRAvr/o f3D44nxfaJhxELqE9HflDdw3T+t8F2E4/qEABKwaV3ZPsWScGRNmkZ8ObkKWY/tkGk1j AULek+VQ1Bx8IKZsgnui5geY/7tGOboczBirqe2+1z96FsmIlwUpqsktYdW9CVQbQyuv +N0A== MIME-Version: 1.0 Received: by 10.112.49.66 with SMTP id s2mr4842470lbn.88.1344825589196; Sun, 12 Aug 2012 19:39:49 -0700 (PDT) Received: by 10.112.89.174 with HTTP; Sun, 12 Aug 2012 19:39:49 -0700 (PDT) In-Reply-To: <502865AB.7090808@moonspot.net> References: <50280D37.10109@moonspot.net> <502842C1.1070401@moonspot.net> <502865AB.7090808@moonspot.net> Date: Sun, 12 Aug 2012 20:39:49 -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 8:25 PM, Brian Moon wrote: >>> 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. > > > Could you show the whole class that this public function belongs to? I don't > see how this function fits in and compares at all to the class you linked. > > Brian. It renders the iterator class completely obsolete; it could be part of the Binary Search Tree class. If you still wanted a separate class (separation of concerns, perhaps) it would look something like this (not sure on exact yield syntax): class PostOrderIterator implements IteratorAggregate { /** * @var BinaryTree */ protected $root; public function __construct(BinaryTree $root) { $this->root = $root; } /** * @link http://php.net/manual/en/iteratoraggregate.getiterator.php * @return Traversable */ public function getIterator() { yield $this->traverse($this->root); } private function traverse(BinaryTree $node) { if ($node->left) { yield* $node->left; } if ($node->right) { yield* $node->right; } yield $node->value; } } All this does is take the node that acts as root in it's constructor and then calls the generator with root as the starting point. That's 34 lines of code including comments and whitespace.