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]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php