Re: Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...)
| From: | Bruce Weirdan | Date: | Sun, 30 Aug 2020 18:01:42 +0000 |
| Subject: | Re: Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...) | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111729@lists.php.net to get a copy of this message | ||
> On Sun, Aug 30, 2020 at 6:13 PM tyson andre <tysonandre775@hotmail.com>
> wrote:
>
>> > I like this, but I do not like the flags. I don't think they're at all
>> useful. A lot of the other discussion in the thread seems to be needlessly
>> complicating it, too.
>> >
>> > all() and any() only need return booleans. Their callbacks only need
>> return booleans. That's the point. first() makes sense to add, and it
>> would return the first value that matches.
>>
>> What would first() return on failure? Would it throw (inefficient)?
>> Would it set an optional output reference to distinguish between
>> returning the value null from an iterable and the null from no matches?
>>
>
> If it took the default value as well it could return that. While it's
> useful in itself it also would enable you to pass a marker object and check
> the identity of that to know if no matches have been found:
>
> $none = new stdClass;
> $element = first($collection, fn($elt) => ...);
> if ($element === $none) {
> // nothing found
> }
>
Of course it should have been `$element = first($collection, fn($elt)
=>..., $none);`
--
Best regards,
Bruce Weirdan mailto:
weirdan@gmail.com