On Mon, Sep 7, 2015 at 6:22 AM, Rowan Collins <rowan.coll...@gmail.com> wrote: > 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); > } > };
It's really bothering me that you have a mismatched curly brace. :D > 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. Sure, I didn't defend the choices too much because the post was already lengthy. If you care only that the last argument in the chain is the input then your definition would be just fine. You are right that these functions would likely be named, but if we keep the currying pattern the short-form closures are still helpful: function reduce($initial) { return $fn ~> $input ~> { $accumulator = $initial; foreach ($input as $value) { $accumulator = $fn($accumulator, $value); } return $accumulator; }; } If this RFC passes I'd like to see one that builds on it to remove the { return <expr> ;} boilerplate of the above definition: function reduce ($initial) ~> $fn ~> $input { $accumulator = $initial; foreach ($input as $value) { $accumulator = $fn($accumulator, $value); } return $accumulator; } I'm not saying it would be that syntax exactly but something like that would be nice if this RFC passes. >> 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. I guess we just disagree there. I have no further comment on it. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php