Re: Arrow function expressions in PHP
| From: | Rowan Collins | Date: | Thu, 01 Oct 2015 18:05:53 +0000 |
| Subject: | Re: Arrow function expressions in PHP | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-88617@lists.php.net to get a copy of this message | ||
Levi Morrison wrote on 01/10/2015 18:38:
Chain works is because the functions passed to it accept iterables as their only parameter and return either an another iterable or a reducing function (in this example sum is the reducer). This is why the functions are returning closures that accept only one parameter.OK, that makes sense of one level of indirection. It doesn't however seem to justify the nesting in reduce() If I understand correctly, your library obliges me to write this:
$algorithm = chain(
map(function($x) => $x * 2),
reduce(0)(function($acc, $val) => $acc + $val)
);
$algorithm([1,2,3]);
But that seems to have no advantage over this:
$algorithm = chain(
map(function($x) => $x * 2),
reduce(0, function($acc, $val) => $acc + $val)
);
$algorithm([1,2,3]);
Which would make the body of reduce significantly simpler, because it's just one named function accepting a closure, and returning a closure, with no further nesting.
The function chaining only appears to be useful if the closure returned by reduce(0) is to be passed or applied in some other way, and I can't think of when that would be required.
Regards,
--
Rowan Collins
[IMSoP]