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

[8.x] Add Collection@isSingle method #36428

Merged
merged 1 commit into from
Mar 2, 2021

Conversation

JosephSilber
Copy link
Member

@JosephSilber JosephSilber commented Mar 2, 2021

Adds an isSingle method to the collections:

collect([])->isSingle();     // false
collect([1])->isSingle();    // true
collect([1, 2])->isSingle(); // false

Calling $collection->count() === 1 is so common, I believe it makes sense to have a separate expressive method for it (even more so for the lazy collection, which is a tad more tricky).

*/
public function isSingle()
{
return $this->take(2)->count() === 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

why is take(2) necessary? :)

Copy link
Member Author

@JosephSilber JosephSilber Mar 2, 2021

Choose a reason for hiding this comment

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

@gocanto without it, the whole collection would have to be enumerated, which is a waste.

As soon as we know that there are 2, we know that it's not single!


See this test, which ensures we don't enumerate more than 2 items:

public function testIsSingleIsLazy()
{
$this->assertEnumerates(2, function ($collection) {
$collection->isSingle();
});
}

Without take(2), this test would fail.

Copy link
Contributor

Choose a reason for hiding this comment

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

cool stuff.

@browner12
Copy link
Contributor

idk, seems like overkill to me.

if(collect([1])->count() === 1)) 

is super readable and pretty terse already.

@taylorotwell taylorotwell merged commit b72178d into laravel:8.x Mar 2, 2021
@ahinkle
Copy link
Contributor

ahinkle commented Mar 2, 2021

Taylor updated this to containsOneItem(); 5b7ffc2

@stancl
Copy link
Contributor

stancl commented Mar 2, 2021

If you want syntax improvements to ->count() === $x why not go with ->countIs($x) or even ->count($x)

if ($collection->count() === 1)

if ($collection->countIs(1))

if ($collection->count(1))

if ($collection->isSingle())

if ($collection->containsOneItem())

containsOneItem() or isSingle() makes this work only for a single use case without really improving readability IMO

@JosephSilber JosephSilber deleted the collection/is-single branch March 2, 2021 16:50
@LucaRed
Copy link
Contributor

LucaRed commented Mar 4, 2021

$collection->count() === 1;

$collection->containsOneItem();

Hmm. It's even longer than the normal comparison. I find it less readable.

+1 for a general method such as countIs($n).

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.

7 participants