Skip to content

[5.3] Add partition collection method#16627

Merged
taylorotwell merged 4 commits into
laravel:5.3from
freekmurze:feature-partition
Dec 3, 2016
Merged

[5.3] Add partition collection method#16627
taylorotwell merged 4 commits into
laravel:5.3from
freekmurze:feature-partition

Conversation

@freekmurze
Copy link
Copy Markdown
Contributor

@freekmurze freekmurze commented Dec 2, 2016

The partition method returns a collection with two elements. Items in the first element did pass the given $callback, items in the second element did not.

Here's an example:

collect(range(1, 10))->partition(function ($i) {
    return $i <= 5;
});

// returns a new collection
// first element is a collection with items 1, 2 ,3, 4, 5
// second element is a collection with items 5, 6, 7, 8, 9, 10

@freekmurze freekmurze changed the title Add partition collection method [5.3] Add partition collection method Dec 2, 2016
foreach ($this->items as $item) {
$partitions[! (int) $callback($item)][] = $item;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shouldn't you just return the plain array so you can separate them using lists?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yup, changed it to output a plain array.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, list works on collections, so I think we should definitely return a collection.

Really no collection method should ever return a plain array. You don't ever want to break the chain.

@GrahamCampbell
Copy link
Copy Markdown
Collaborator

Looks cool. It should be noted that the strict definition of a partition requires it to be non-empty, though, I guess it's probably ok in practical terms.

return $this->slice(($page - 1) * $perPage, $perPage);
}

/**
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please format like the other phpdoc, then looks good. 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm afraid I don't see the formatting problem. Could you tell what's wrong with the current formatting?

Copy link
Copy Markdown
Collaborator

@GrahamCampbell GrahamCampbell Dec 2, 2016

Choose a reason for hiding this comment

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

Short descriptions must be "sort". Exactly one sentence and one line. The rest must be separated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Got it, thanks for clarifying

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.

4 participants