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] Create Collection chunkInto method #35295

Merged
merged 3 commits into from Nov 23, 2020
Merged

Conversation

browner12
Copy link
Contributor

@browner12 browner12 commented Nov 19, 2020

This methods defines the number of chunks we would like to end up with. It fills all non-last chunks first, before placing the remainder in the final chunk.

This method is similar to, but different than, split().

Given the following Collection:

$array = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

$array->split(3) will return 3 chunks of sizes 4, 3, and 3.

$array->chunkInto(3) will return 3 chunks of sizes 4, 4, and 2.

this methods defines the number of chunks we would like to end up with. it fills all non-last chunks first, before placing the remainder in the final chunk.
@taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented Nov 20, 2020

Can you explain in simple terms how it is fundamentally different than split and when you would use one over the other?

@browner12
Copy link
Contributor Author

@browner12 browner12 commented Nov 20, 2020

split will make it so each chunk never differs in count from another chunk by more than 1.

Screen Shot 2020-11-20 at 9 37 16 AM

chunkInto will fill all earlier chunks completely before allocating the remainder to the final chunk.

Screen Shot 2020-11-20 at 9 37 10 AM

On one of our sites, we have a sitemap page with a list of all the pages on the site. I wanted to display them in columns, and I wanted it display like in the second picture. This is where chunkInto handles what I need, and split does not.

@GrahamCampbell GrahamCampbell changed the title [8.x] create Collection chunkInto method [8.x] Create Collection chunkInto method Nov 20, 2020
@taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented Nov 21, 2020

One thing giving me a bit of pause is we have used the "into" language to indicate something will be injected into a class... mapInto and pipeInto for example.

@browner12
Copy link
Contributor Author

@browner12 browner12 commented Nov 21, 2020

Gotcha. Yah, I'm totally open to other names. Maybe sticking with the split prefix is more appropriate, too, since chunk defines the number of items in the chunk, and split defines the number of chunks. This new method just allots them differently.

  • splitBy
  • splitIn

Another option would be to pass a second parameter flag to split() although I know we're not normally too keen on that.

@taylorotwell
Copy link
Member

@taylorotwell taylorotwell commented Nov 21, 2020

I think I'm fine with splitIn.

browner12 added 2 commits Nov 21, 2020
forgot the LazyCollection.
@taylorotwell taylorotwell merged commit ff79432 into laravel:8.x Nov 23, 2020
13 checks passed
@browner12 browner12 deleted the chunk-into branch Nov 23, 2020
*/
public function splitIn($numberOfGroups)
{
return $this->chunk(ceil($this->count() / $numberOfGroups));
Copy link
Member

@JosephSilber JosephSilber Jan 13, 2021

Choose a reason for hiding this comment

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

This enumerates the whole collection twice 😢

Copy link
Contributor Author

@browner12 browner12 Jan 13, 2021

Choose a reason for hiding this comment

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

I'll admit I don't understand the lazy collections as well, so if there's a better way to do this, definitely open to it.

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

3 participants