Re: [RFC] [Discussion] Short Closures
| From: | Rowan Collins | Date: | Mon, 07 Sep 2015 17:30:23 +0000 |
| Subject: | Re: [RFC] [Discussion] Short Closures | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88102@lists.php.net to get a copy of this message | ||
Levi Morrison wrote on 07/09/2015 17:56:
function reduce ($initial) ~> $fn ~> $input ~> {
$accumulator = $initial;
foreach ($input as $value) {
$accumulator = $fn($accumulator, $value);
}
return $accumulator;
}
Hm, this is an interesting idea. I actually think the case for it is *stronger* if ~> was considered special syntax for trivial functions, because if not, there's this odd hybrid, which we probably wouldn't want to allow:
function weird_declaration($bar) ~> {
any();
code();
here();
}
Whereas this declaration might be quite useful:
function double($x) ~> $x * 2;
I realise the syntax was only an example, but this way there'd be a nice consistency between named and anonymous functions.
I think that would leave your example as:
function reduce ($initial) ~> $fn ~> function($input) use ( $initial, $fn) {
$accumulator = $initial;
foreach ($input as $value) {
$accumulator = $fn($accumulator, $value);
}
return $accumulator;
}
Regards,
--
Rowan Collins
[IMSoP]