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
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to