Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88099 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32086 invoked from network); 7 Sep 2015 16:56:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Sep 2015 16:56:08 -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.160.178 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.160.178 mail-yk0-f178.google.com Received: from [209.85.160.178] ([209.85.160.178:32783] helo=mail-yk0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3A/60-27499-7A1CDE55 for ; Mon, 07 Sep 2015 12:56:07 -0400 Received: by ykei199 with SMTP id i199so85733149yke.0 for ; Mon, 07 Sep 2015 09:56:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=NAUW2SVk1SPqSpqRnsUbTw77gjUzQh0kRh1Qv2ezPyY=; b=v5UOFALpft99SsPkfT57TAamKr+JMBdK+NJyeEU1Hpt3Drc9ra2hOdxxj18mi964AQ DCkyhuSrP82Ne9LNRwPItrp8k1mqAGWh3BSF7fBcygdCBXec43eUThG/O1R4aCJUnxjA KmRfVWWuagLv86aVlSf3W5xPVPGqY8SkBD/KcvswAhS2mzbDWRdChsDtRBEEEUea1x8O 9DxWYP/CDVyNLz0KjQm6BMg48GoKO+Rh2AQQ5Y6NZ5Js9msCQYcjYfWxb+OASt5CeRrt hQD7Es3u2oZ475lWAUGBg1M0Ao8ZbgRW0DrWWkqrBFqfhzz98RQuKdm94Tdv2+JVMyci aY/w== MIME-Version: 1.0 X-Received: by 10.170.127.203 with SMTP id t194mr21091165ykb.41.1441644964688; Mon, 07 Sep 2015 09:56:04 -0700 (PDT) Sender: morrison.levi@gmail.com Received: by 10.31.41.205 with HTTP; Mon, 7 Sep 2015 09:56:04 -0700 (PDT) In-Reply-To: <55ED817B.5030109@gmail.com> References: <55E77CA9.7050609@gmail.com> <55E81959.6040305@gmail.com> <55ED817B.5030109@gmail.com> Date: Mon, 7 Sep 2015 10:56:04 -0600 X-Google-Sender-Auth: uzrC1fcShJJIgHx6WKhoBgHNcks Message-ID: To: Rowan Collins Cc: internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] [RFC] [Discussion] Short Closures From: levim@php.net (Levi Morrison) On Mon, Sep 7, 2015 at 6:22 AM, Rowan Collins wrote: > Levi Morrison wrote on 04/09/2015 18:34: >> >> Hopefully I've been able to demonstrate that this style of coding is >> powerful and that the chaining of closures was helpful. > > > Sort of - the chaining is far from self-explanatory, and some of it is still > rather artificial. For instance, surely map would in reality have been a > named function: > > function map($fn) { > return $input ~> { > foreach ($input as $key => $value) { > yield $key => $fn($value); > } > }; It's really bothering me that you have a mismatched curly brace. :D > Reduce can also be simplified to a named function and a single closure: > > function reduce($initial, $fn) > return $input ~> { > $accumulator = $initial; > foreach ($input as $value) { > $accumulator = $fn($accumulator, $value); > } > return $accumulator; > }; > } > > > I realise you never claimed these functions were as simple as possible, or a > perfect example of using higher-order functions, but right now I can't see > why you'd write code like that other than as a coding exercise. Sure, I didn't defend the choices too much because the post was already lengthy. If you care only that the last argument in the chain is the input then your definition would be just fine. You are right that these functions would likely be named, but if we keep the currying pattern the short-form closures are still helpful: function reduce($initial) { return $fn ~> $input ~> { $accumulator = $initial; foreach ($input as $value) { $accumulator = $fn($accumulator, $value); } return $accumulator; }; } 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. >> Some people have also suggested removing the block syntax for short >> closures. The implementation of reduce as defined above is a >> demonstration of why the block syntax is helpful: >> >> $reduce = $initial ~> $fn ~> $input ~> { >> $accumulator = $initial; >> foreach ($input as $value) { >> $accumulator = $fn($accumulator, $value); >> } >> return $accumulator; >> }; >> >> With the block syntax removed the last closure in the chain has to use >> long-form like this: >> >> $reduce = $initial ~> $fn ~> function($input) use($initial, $fn) { >> $accumulator = $initial; >> foreach ($input as $value) { >> $accumulator = $fn($accumulator, $value); >> } >> return $accumulator; >> }; >> >> I hope you'll agree with me that this is just weird. > > > Not really, no. The difference in behaviour of variable scope makes the > use() statement look out of place, but the fact that there are two trivial > functions and one complex one, and therefore two different pieces of syntax, > seems perfectly reasonable. I guess we just disagree there. I have no further comment on it.