Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88091 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 3731 invoked from network); 7 Sep 2015 12:23:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Sep 2015 12:23:23 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.173 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.173 mail-wi0-f173.google.com Received: from [209.85.212.173] ([209.85.212.173:35425] helo=mail-wi0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id ED/00-03021-AB18DE55 for ; Mon, 07 Sep 2015 08:23:23 -0400 Received: by wicge5 with SMTP id ge5so82180140wic.0 for ; Mon, 07 Sep 2015 05:23:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=2NfGjMah9+JO7HFPOTaUZgfaGyrVJlpgYByw4gIqq+Q=; b=p2aJ+DER+2VIOwkGDZpMI5Xiop8mu7c5AEXAZo+xi6D3zOKzb+3LvkTODWLaClc/q1 MfweNMgDTAkyI0pmclKmfEE3K7hRVQr69jeLrAOLvUQI/6hC/U9sojGXNOouFzli1vNt MN3Wy8RtJAfDf11T/sEimBNqhw/pqsVVTTD/d9X11Jm67JlZx86ygeEX66S+ImPtkO0T tCPN8pf18iWcR5SD9Is6rv+BDcAP2LXg0HWoMw6he/zhdofOaK/GtZuK/1aQwN9yFow+ Jza8fJv4eFFnoXJMuf7LMnx+vXipFGASI4DWKJz3rsd+OYVckH9W9blmFIWOMRCYbbEB srCQ== X-Received: by 10.194.78.34 with SMTP id y2mr31020732wjw.91.1441628599858; Mon, 07 Sep 2015 05:23:19 -0700 (PDT) Received: from [192.168.0.134] ([62.189.198.114]) by smtp.googlemail.com with ESMTPSA id mz12sm55034wic.4.2015.09.07.05.23.18 for (version=TLSv1/SSLv3 cipher=OTHER); Mon, 07 Sep 2015 05:23:19 -0700 (PDT) To: internals@lists.php.net References: <55E77CA9.7050609@gmail.com> <55E81959.6040305@gmail.com> Message-ID: <55ED817B.5030109@gmail.com> Date: Mon, 7 Sep 2015 13:22:19 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] [Discussion] Short Closures From: rowan.collins@gmail.com (Rowan Collins) 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); } }; 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. > 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. Regards, -- Rowan Collins [IMSoP]