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

Add Collection::isEmpty Method #130

Merged
merged 3 commits into from
Jul 8, 2021
Merged

Add Collection::isEmpty Method #130

merged 3 commits into from
Jul 8, 2021

Conversation

AlexandruGG
Copy link
Collaborator

@AlexandruGG AlexandruGG commented Jul 7, 2021

Overview

This PR adds the isEmpty method and IsEmptyable interface.

Why is this needed when we have Collection::count?

1. Convenience

Typical use case:

$collection = Collection::fromIterable([1, 2, 3]);
$filteredCollection = $collection->filter(...);

if (!$filteredCollection->isEmpty()) {
    // do something
}

// compared to

if (0 !== $filteredCollection->count()) {
    // do something
}

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 and Collection::range(0, 100_000_000):

$isEmpty = 0 === $collection->count();
// vs
$isEmpty = $collection->isEmpty();

Ran it 100,000 times for isEmpty(), only 10 times for count, 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

  • Tests
  • Static analysis check
  • Documentation

@AlexandruGG AlexandruGG marked this pull request as ready for review July 7, 2021 22:57
@AlexandruGG
Copy link
Collaborator Author

@drupol I hope you'll like this one 😁

Copy link
Collaborator

@drupol drupol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok for it just a minor thing to do and we are good.

docs/pages/api.rst Outdated Show resolved Hide resolved
src/Collection.php Show resolved Hide resolved
@drupol drupol merged commit 30e53b0 into master Jul 8, 2021
@drupol drupol deleted the feature/is-empty-method branch July 8, 2021 05:44
@drupol
Copy link
Collaborator

drupol commented Jul 8, 2021

Thanks, good addition !

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

Successfully merging this pull request may close these issues.

None yet

2 participants