On Fri, 14 Feb 2020 at 01:39, Mike Schinkel <m...@newclarity.net> wrote:

> > On Feb 13, 2020, at 7:24 PM, Rowan Tommins <rowan.coll...@gmail.com>
> wrote:
> >
> > An idea I had earlier which might solve some of them is if what was
> returned was not a normal Closure instance, but a new class like
> FunctionReference. It could then "remember" the name of the function
> wrapped, and implement __toString, Serializable, etc. It could inherit from
> Closure, so instanceof checks would work, and bindTo would return a normal
> Closure. I'm sure there's downsides I haven't thought of yet, but I thought
> I'd throw the idea into the mix.
>
> I thought about that too, and mentioned it yesterday in a reply[1] to you
> on this list.
>
> Here is the link to the Gist with the hypothetical code using such a
> concept:
>
> - https://gist.github.com/mikeschinkel/78684d708358e1d101e319c7a2fdef9c
>
> -Mike
>
>
> [1] https://www.mail-archive.com/internals@lists.php.net/msg100719.html
>
>
Maybe, bettern fn:  * fn(callable $to_clsoure ): *closure ?

I know fn is used in arrow functions. However, for that same reason fn is
the most convenient.

Using dik examples( by the way, they are brilliant ):

$result = Stats::of($string)
    ->analyze(fn(normalizeNewlines))
     ->analyze(fn(readingLevel))
     ->analyze(fn(countStats))
     ->analyze(fn($synonymSuggester, 'analyze'))
     ->analyze(fn(WordCouter::class, 'analyze'))
     ->analyze(fn($s) => wordDistribution($s, 3))

Returning to ::function again and with a modified example of dik( again ):

Now is:

result = Stats::of($string)
    ->analyze('\Stats\Analyzer\normalizeNewlines')
    ->analyze('\Stats\Readings\readingLevel')
    ->analyze('\Stats\Parser\countStats')
    ->analyze(fn($s) => \Stats\Analyzer\wordDistribution($s, 3));

Could be:
use function Stats\Analyzer\normalizeNewlines;
use function \Stats\Readings\readingLevel;
use function \Stats\Parser\countStats;

result = Stats::of($string)
    ->analyze(normalizeNewlines::function)
    ->analyze(readingLevel::function)
    ->analyze(countStats::function )
    ->analyze(fn($s) => \Stats\Analyzer\wordDistribution($s, 3));

Maybe '::function' is something long, however it wiil be autocompleted by
editors and, moreover, nowadays function is a reserved keyword.  Could we
use "normalizeNewlines::name", but in this case: normalizeNewlines is
function or class ?, because in this case could be "normalizeNewlines" a
class and "name" a constant of class.

Rowan put me in an awkward situation with [MyClass::class, method::function
] however, in this case, could be simply MyClass::class, 'method' ].
Because method don't need be used with namespace. ::function could be
documented with advice of being used  with name of functions. Because
::function is also useful in array:

$filters = [ normalizeNewlines::function, readingLevel::function,
countStats::function
]
$content = Stats::of($string);
foreach($filters as $filter) $filter($content);

Regards

Reply via email to