Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR adds the
isEmpty
method andIsEmptyable
interface.Why is this needed when we have
Collection::count
?1. Convenience
Typical use case:
Other popular PHP collection libraries also have this, so people are used to it:
Doctrine: https://www.doctrine-project.org/projects/doctrine-collections/en/1.6/index.html#isempty
Laravel: https://laravel.com/docs/8.x/collections#method-isempty
Knapsack: https://dusankasan.github.io/Knapsack/#Operations-isEmpty
Other languages have this:
Java: https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html#isEmpty--
C++: https://www.cplusplus.com/reference/vector/vector/empty/
Haskell: http://zvon.org/other/haskell/Outputprelude/null_f.html
2. Performance
This is a big one. Simple benchmark using
microtime
andCollection::range(0, 100_000_000)
:Ran it 100,000 times for
isEmpty()
, only 10 times forcount
, and here's why (average time for a single run):isEmpty
-> 5.575289726257324E-6 seconds (basically instant because it's in constant time)count
-> 13.571631002426148 seconds (this runs in O(N))Checklist