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]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to