Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:87181 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 16021 invoked from network); 15 Jul 2015 11:28:41 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jul 2015 11:28:41 -0000 Authentication-Results: pb1.pair.com smtp.mail=tjerk.meesters@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tjerk.meesters@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.177 as permitted sender) X-PHP-List-Original-Sender: tjerk.meesters@gmail.com X-Host-Fingerprint: 209.85.213.177 mail-ig0-f177.google.com Received: from [209.85.213.177] ([209.85.213.177:32874] helo=mail-ig0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 73/E2-27477-8E346A55 for ; Wed, 15 Jul 2015 07:28:40 -0400 Received: by iggp10 with SMTP id p10so118819874igg.0 for ; Wed, 15 Jul 2015 04:28:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=pIoVR5QU8uXQEGzSlGha5Bu1ecJqV5xm1czuvI/mB0I=; b=uukCbj+31xtTEWEs/MKXC7JUjT33XrWOdtOBXzWkIbPyLj5mi8eOMZQiP9vT9Wx8MO cq87cG+gwkPTtSEpTRG6uQ55aC863hPBjklCnwsWlG3Vdf3M7S0QRCndzL38AdQNyiuu gN8PPpIWbDmBdc6R/453OifIRmHNLNwyTuUEobk8R8WOFjx1cVbk2pmtm3CQySoPXNOh JlJQtvpHLDcYEayrx0Tb6i6mM00H+h61Gm1WI30pjX/stjk1P6op3enpJwoG+uaJhbAU DIF59Z8SRNTqYvWXirAN7xXEI/YwnODeY4EQIMh26VH0efOqfPMOrhgXKdf+G6zCHwbm rYyQ== X-Received: by 10.50.136.134 with SMTP id qa6mr25476031igb.26.1436959718193; Wed, 15 Jul 2015 04:28:38 -0700 (PDT) MIME-Version: 1.0 Date: Wed, 15 Jul 2015 11:28:28 +0000 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary=089e0141a8dc8f5bfb051ae8419f Subject: Consolidation of Traversable and array operations From: tjerk.meesters@gmail.com (Tjerk Meesters) --089e0141a8dc8f5bfb051ae8419f Content-Type: text/plain; charset=UTF-8 Hi! A few weeks ago I resurrected a two year old proposal for adding two array functions, namely array_every() and array_some(), modelled after their JavaScript equivalent. https://github.com/php/php-src/pull/1385 The most notable comment was that it would be nice to support Traversable as well as arrays; instead of only supporting this for my own functions, I've generalised this so that other functions can take advantage of this as well. These are the additions: 1. A ZPP argument; the "t" (for traversable) argument type has been added that checks for either an array or implementation of zend_ce_traversable. 2. A generic iteration function, called php_traverse(); it accepts: a. the zval* to iterate over, b. a step-wise function that receives the value and key, and returns a boolean that determines whether iteration should continue or not, c. a traversal mode (only values, or keys and values), d. a context that's sent to the step-wise function. 3. A concrete implementation of a step-wise iteration function, php_traverse_until, that gets called as part of array_every() and array_some(). The implementation of above proposal can be found in the PR itself. With more functions adopting this idea, you could see this work: var_dump(array_reduce((function() { yield 1; yield 2; yield 3; })(), function($total, $current) { return $total + $current; }, 0); // int(6) Let me know what you think! --089e0141a8dc8f5bfb051ae8419f--