Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61160 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 48654 invoked from network); 12 Jul 2012 08:46:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jul 2012 08:46:28 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@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: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.217.170 mail-lb0-f170.google.com Received: from [209.85.217.170] ([209.85.217.170:35132] helo=mail-lb0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 46/60-46517-3EE8EFF4 for ; Thu, 12 Jul 2012 04:46:28 -0400 Received: by lbgc1 with SMTP id c1so3309019lbg.29 for ; Thu, 12 Jul 2012 01:46:24 -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:content-transfer-encoding; bh=1TKi+d3d6PxhbDbEDhokg13oKfG+EdQ/MgaVpSYmY/4=; b=hsReJHH7PbzWfSHsJ5ZLUcK4ZDCsSwOf4t9G5upewngrjT4g6gbAL35oq/rbpPwV42 z2B5gF3cCxbVrLYtDdmic9AOvJ0sqiVQRPu9agG2C3dAfH5v+gFYeks0Kj2Gsl5cZsvj 4efaJeC3m8nERliCMmkgBZSMZdBjIzsFkr4sPNE3jr0wd9chO5LkDwBBZVjn0PRIn2zB krdBm4CnMy5Z7/UOuIJylRwc+pPebqslUN7JN+4YbI/jXDdu/injSXR8moEo03wORyPN rmG+s95ZTdyJJ+uq/LF99My2BNzvACjN2Jd2PAPurw3h4YzqFJ7ox4cT2zlxx/Nz1Exg SBfQ== MIME-Version: 1.0 Received: by 10.152.125.116 with SMTP id mp20mr52088839lab.19.1342082784117; Thu, 12 Jul 2012 01:46:24 -0700 (PDT) Received: by 10.152.114.70 with HTTP; Thu, 12 Jul 2012 01:46:24 -0700 (PDT) In-Reply-To: <1342050547.1456.33.camel@guybrush> References: <1342050547.1456.33.camel@guybrush> Date: Thu, 12 Jul 2012 10:46:24 +0200 Message-ID: To: =?ISO-8859-1?Q?Johannes_Schl=FCter?= Cc: PHP internals Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Internal iteration API From: nikita.ppv@gmail.com (Nikita Popov) On Thu, Jul 12, 2012 at 1:49 AM, Johannes Schl=FCter wrote: > One thing to keep in mind when doing this is to think about consistency. > Right there's quite a distinction. Things either take an array or a > Traversable object. We should think about not creating a mess when some > functions, especially ones called array_foo() allow Traversable while > others won't. So we might need the same infrastructure in regards to > ArrayAccess to help this a bit. Yes, I agree that overloading the array_* functions would be a bit strange (but not *that* strange either). My personally favorite approach would be to create new functions like sum(), product() and reduce(), which would be array_sum(), array_product() and array_reduce() equivalents, but accepting anything iterable as input. The old array_* functions would obviously stay (maybe as aliases, maybe complete separate). For the array_map and array_filter I would do the same (i.e. create map() and filter()), which would return an array when passed an array, but return a (lazy) iterator when passed an iterator. > * array_filter() - This is almost the same as array_map() but we > can't implement it using Traversable, as current() (for good > reasons) returns no reference. I'm not sure how array_filter() and references relate (maybe you meant array_walk?), but in any case: get_iterator style object iterator *can* support references. The function has a by_ref flag. But currently userland iterators can't make use of this. This limitation can be easily removed though, as far as I know. It is just a leftover from previously existing technical limitations regarding interfaces and references. > Another "fun fact" might be that Traversable doesn't require keys to be > unique. This can have strange results for things like array_map(). Yeah, that's one of the reasons why I would prefer map() to return an iterator if an iterator is passed in. Another reason is that iterators can be infinite, which obviously doesn't play nice when returning an array. (And even if they aren't infinite, they may be large). > Ah, and maybe completely unrelated to the things above but not > to forget: When implementing this the code becomes more complex > as exceptions thrown in key(), current(), rewind() have to be > caught. With "classic" zend_hash iteration those operations will > succeed if there was a next element pointer. Maybe that can be > handled in a generic infrastructure, so we have less errors > later. Yeah, you're right about that. It wouldn't be nice if one had to check for errors after every single call :/ Nikita