Re: Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...)
| From: | Mike Schinkel | Date: | Sun, 30 Aug 2020 06:31:35 +0000 |
| Subject: | Re: Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...) | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111717@lists.php.net to get a copy of this message | ||
> On Aug 29, 2020, at 7:18 PM, tyson andre <tysonandre775@hotmail.com> wrote:
>
> Hi Alex,
>
>> I like it!
>>
>> What is the $use_flags parameter for?
>
> It's for deciding what parameters to pass to callback, like
> https://www.php.net/array_filter - the reflection name should
> probably just be $flag
>
> Occasionally, the key might be useful to check against, e.g. the identifier of an item in a
> map, or SplObjectStorage.
>
> - the default is
any($iterable, fn ($value) => ...)
> - or any($iterable, fn ($key) => expr, ARRAY_FILTER_USE_KEY)
> - or any($iterable, fn ($value, $key) => expr,
> ARRAY_FILTER_USE_BOTH)
Doesn't the occasional required use of long constant names like ARRAY_FILTER_USE_KEY and
ARRAY_FILTER_USE_BOTH somewhat negative the benefit of a more concise syntax?
I know that many existing functions in PHP have a collection of long constant names that can be
passed, but is that a style of programming that PHP really wants to continue to proliferate, or
would it not be better to address that use-case in a different manner? (Admittedly doing so might
require having this feature dependent on other critical path language features.)
One approach might be if PHP could allow for skipped parameters in callables — where I am using a
single underscore to indicate an unused parameter (GoLang has a "blank identifier" for
similar purposes so there is some prior art to consider) — e.g:
- the default is any($iterable, fn ( $value ) => ...)
- or any($iterable, fn ( _, $key ) => expr )
- or any($iterable, fn ( $value, $key ) => expr )
PHP could handle it more generically, is possible, or recognize this and branch to different
implementations of any() and all() if the goal is to optimize performance by only passing one value
in key-only or value-only calls.
Optionally we could use null instead:
- the default is any($iterable, fn ($value) => ...)
- or any($iterable, fn (null, $key) => expr )
- or any($iterable, fn ($value, $key) => expr )
-Mike
[1] https://www.geeksforgeeks.org/what-is-blank-identifierunderscore-in-golang/#:~:text=_(underscore)%20in%20Golang%20is,Blank%20Identifier.
<https://www.geeksforgeeks.org/what-is-blank-identifierunderscore-in-golang/#:~:text=_(underscore)%20in%20Golang%20is,Blank%20Identifier.>