> I think a possible improvement might be a generalised syntax, similar to
> that used for constants, that lets you use any \Closure object to define a
> function or method. Thus:
>
>     function reduce = $initial ~> $fn ~> $input ~> {
>         // ...
>     };

I like this but we're getting a bit too far in the future RFC
territory. But bringing this back to the current proposal I think that
the curly brace syntax should stay. I much prefer this:

    function reduce = $initial ~> $fn ~> $input ~> {
        $accumulator = $initial;
        foreach ($input as $value) {
            $accumulator = $fn($accumulator, $value);
        }
        return $accumulator;
    };

To this:

    function reduce = $initial ~> $fn ~> function($input) use($initial, $fn) {
        $accumulator = $initial;
        foreach ($input as $value) {
            $accumulator = $fn($accumulator, $value);
        }
        return $accumulator;
    };

So while assigning the result to a named function (instead of a
variable) is out of scope of this RFC I think it demonstrates the
value of the block syntax which I think we should keep. I've said this
several times now, so I won't say it again :)

> This has that advantage that it isn't just useful for when you want to use
> the short syntax. You can also use it when you're obtaining a Closure in
> some novel way, perhaps a higher-order function:
>
>     // where "add" is a function that adds two numbers
>     function sum = reduce(0)("add");
>
> With this, you don't have to write a wrapper function, or use a global
> variable, to define a proper top-level function.

I like this as well but again we're getting a bit too far away from
the current proposal.

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

Reply via email to