Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add a find method #356

Closed
florentdestremau opened this issue Dec 16, 2022 · 3 comments
Closed

Feature request: add a find method #356

florentdestremau opened this issue Dec 16, 2022 · 3 comments

Comments

@florentdestremau
Copy link

florentdestremau commented Dec 16, 2022

Similar to the javascript Array.prototype.find, this function would take a closure as a an argument and return the first item matching the closure.

Example:

$collection = new ArrayCollection();

$itemToBeFound = $collection->find(fn ($item): bool => $item->getFoo() === 'bar');

$item here would be nullable.

@malarzm
Copy link
Member

malarzm commented Dec 17, 2022

The function is already implemented:

/**
* Returns the first element of this collection that satisfies the predicate p.
*
* @param Closure $p The predicate.
* @psalm-param Closure(TKey, T):bool $p
*
* @return mixed The first element respecting the predicate,
* null if no element respects the predicate.
* @psalm-return T|null
*/
public function findFirst(Closure $p);

@malarzm malarzm closed this as completed Dec 17, 2022
@florentdestremau
Copy link
Author

this is only in the 2.0 version, is there a reason it's not included in earlier versions ?

@florentdestremau
Copy link
Author

ok found this: #252 (comment)

Not completely convinced but okay, fair enough

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants