Re: Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...)
| From: | tyson andre | Date: | Sun, 30 Aug 2020 20:51:04 +0000 |
| Subject: | Re: Proposal: Adding functions any(iterable $input, ?callable $cb = null, int $use_flags=0) and all(...) | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-111732@lists.php.net to get a copy of this message | ||
Hi Bruce Weirdan,
> 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) => ..., $none);
> if ($element === $none) {
> // nothing found
> }
Oh, right, I'd forgotten about that option - that would work.
There's still the question of what it would do without a predicate.
Making the predicate mandatory in the initial proposal would help avoid the confusion of whether
`first() should behave like array_filter() and any() and filter for the first truthy value.
Calling it [iterable_]search_callback() or first_matching() or find() might help distinguish this
from the reset()/end()/next()/prev() family of functions - not happy with any of my naming ideas
Thanks,
- Tyson