Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:87994 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 72320 invoked from network); 1 Sep 2015 14:21:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Sep 2015 14:21:57 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.180 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.180 mail-wi0-f180.google.com Received: from [209.85.212.180] ([209.85.212.180:36153] helo=mail-wi0-f180.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7F/80-64170-484B5E55 for ; Tue, 01 Sep 2015 10:21:57 -0400 Received: by wibz8 with SMTP id z8so34253763wib.1 for ; Tue, 01 Sep 2015 07:21:54 -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=45yPrWaxdzlI/eDzH4S73T8Fr6eOh3kmjWEzpZugNco=; b=PMJ4qEeeUgcp32ntOXO9BTuA9DfOPOLa8T0JFg3iDlP9Z2WW+cLjvGhtUa3LMZ1CPB Ks289sH2rY3Z477F/X+ZhsChsO21mBisadnYk6M/3CU4bp5UrCyB3GW9LVUmRr0bLFv1 QtZsfkbAngN4A34T/j5nxnoho6Bh6j7TJYM5mYP790UrqeeqCreLbgpOMQqcrK3dZPyT iLVZt2nApQ+AOlCmEtU+DHlFiJgGT6Kygnoh4YnBDHhWpvaZhXhHEzcNIMG25AuhxdT4 vycWMkEcyg9YO12Y/IdudtyKsKEUfYuXdEPd7w8gFurFY3mTuLfoGdbfgffTh+U5V/rb KI/g== X-Received: by 10.180.105.36 with SMTP id gj4mr3523805wib.49.1441117314479; Tue, 01 Sep 2015 07:21:54 -0700 (PDT) Received: from [192.168.0.152] ([62.189.198.114]) by smtp.googlemail.com with ESMTPSA id k12sm27506067wjw.4.2015.09.01.07.21.53 for (version=TLSv1/SSLv3 cipher=OTHER); Tue, 01 Sep 2015 07:21:53 -0700 (PDT) To: internals@lists.php.net References: <55E4C19F.4060704@gmail.com> <55E4F029.10104@gmail.com> <7BCB36EE-56C8-441F-BF40-954D9FA912A9@lerdorf.com> <55E51FAA.8030106@gmail.com> Message-ID: <55E5B465.6070004@gmail.com> Date: Tue, 1 Sep 2015 15:21:25 +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) Anthony Ferrara wrote on 01/09/2015 06:53: > function partial(callable $cb) { > return $left ~> $right ~> $cb($left, $right); > } The thing that is most unreadable to me in this is the associativity. Someone from, say, a Haskell background might be used to mentally grouping X -> Y -> Z into a series of steps but to an untrained eye it looks almost like there's a list of terms with ~> as the separator. As I understand it, an expanded form would be: return ($left) ~> { return ($right) ~> { return $cb($left, $right) } }; ...which is a bit easier to mentally group, but not much prettier than current PHP syntax. What about something a bit more "enclosed" but still short. A for-loop-style construct occured to me: # lambda(params; expression) lambda($a, $b; $a * $b) lambda($x; { while(foo()) { $x ++; } return $x; }) The curly braces don't look very natural in that last example, but I'm not sure what else to use. Maybe just use the existing function() syntax if you want a block rather than an expression? A nice expansion of this would be to have an explicit use clause without any extra keywords: # lambda(params; bound vars; expression) $double = lambda($a;; 2*$a) $x=3; $triple = lambda($a; $x; $x * $a) Taking one of the RFC's examples: function sumEventScores($events, $scores) { $types = array_map(lambda($event;; $event['type']), $events); return array_reduce($types, lambda($sum, $type; $scores; $sum + $scores[$type])); } The next example benefits less if we allow only expressions, but you could still write this: function reduce(callable $fn) { return lambda($initial; $fn; function($input) use ($fn, $initial) { $accumulator = $initial; foreach ($input as $value) { $accumulator = $fn($accumulator, $value); } return $accumulator; } ); } I'll leave it to others to judge if this is better or worse than the current proposal, just playing with ideas... Regards, Rowan Collins -- [IMSoP]