Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88104 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 43993 invoked from network); 7 Sep 2015 18:28:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Sep 2015 18:28:43 -0000 X-Host-Fingerprint: 94.7.93.128 unknown Received: from [94.7.93.128] ([94.7.93.128:14754] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8E/91-34134-B57DDE55 for ; Mon, 07 Sep 2015 14:28:43 -0400 Message-ID: <8E.91.34134.B57DDE55@pb1.pair.com> To: internals@lists.php.net References: <55E77CA9.7050609@gmail.com> <55E81959.6040305@gmail.com> <55ED817B.5030109@gmail.com> Date: Mon, 7 Sep 2015 19:28:37 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0 SeaMonkey/2.35 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 94.7.93.128 Subject: Re: [PHP-DEV] [RFC] [Discussion] Short Closures From: ajf@ajf.me (Andrea Faulds) Hi Levi, Levi Morrison wrote: > If this RFC passes I'd like to see one that builds on it to remove the > { return ;} boilerplate of the above definition: > > function reduce ($initial) ~> $fn ~> $input { > $accumulator = $initial; > foreach ($input as $value) { > $accumulator = $fn($accumulator, $value); > } > return $accumulator; > } > > I'm not saying it would be that syntax exactly but something like that > would be nice if this RFC passes. That looks rather weird to me, but I like the concept. I think a possible improvement might be a generalised syntax, similar to that used for constants, that lets you use any \Closure object to define a function or method. Thus: function reduce = $initial ~> $fn ~> $input ~> { // ... }; This has that advantage that it isn't just useful for when you want to use the short syntax. You can also use it when you're obtaining a Closure in some novel way, perhaps a higher-order function: // where "add" is a function that adds two numbers function sum = reduce(0)("add"); With this, you don't have to write a wrapper function, or use a global variable, to define a proper top-level function. Hope this is helpful. -- Andrea Faulds http://ajf.me/