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

[5.1] Add split method into collection #10889

Closed
wants to merge 1 commit into from

Conversation

arrilot
Copy link
Contributor

@arrilot arrilot commented Nov 11, 2015

Use case:
I need to split a collection of arbitary length into 3 columns of relatively equal length.

Notes:

  1. Maybe divide is a better name?
  2. Should we care about preserving the keys? In this implementation they never preserve.

@arrilot
Copy link
Contributor Author

arrilot commented Nov 11, 2015

ping @JosephSilber

@JosephSilber
Copy link
Member

How is this different than the existing chunk method?

@arrilot
Copy link
Contributor Author

arrilot commented Nov 11, 2015

This splits the collection into N pieces.
Chunk breaks the collection into collections with N items each

@@ -820,6 +820,25 @@ public function splice($offset, $length = null, $replacement = [])
}

/**
* Split the collection into several pieces.
*
* @param int $amount
Copy link
Member

Choose a reason for hiding this comment

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

cs

@JosephSilber
Copy link
Member

So it's the same as this?

$users->chunk(ceil($users->count() / $n));

...and this even preserves the keys.

@arrilot
Copy link
Contributor Author

arrilot commented Nov 11, 2015

There is a small difference - there are always exactly N pieces in the result collection
collect([1,2])->chunk(ceil($users->count() / 3))->toArray();
gives

[
   [1],
   [2],
]

collect([1,2])->split(3)->toArray();
gives

[
   [1],
   [2],
   [],
]

It's way more simple and clear too

@JosephSilber
Copy link
Member

forPage calls slice, which calls array_slice with the $preserve_keys flag set to true.

Why doesn't it preserve the keys?

@arrilot
Copy link
Contributor Author

arrilot commented Nov 11, 2015

@JosephSilber
Copy link
Member

That's been changed in 5.2.

The slice method now preserves keys, not even giving you the choice to override it.

We're moving away from all these Boolean flags. Wherever possible, keys should be preserved. If you don't want that, call values on the result (recursively if needed).

@arrilot
Copy link
Contributor Author

arrilot commented Nov 11, 2015

Oh, as well as chunk I see

So it doesn't preserve in 5.1 but gonna preserve in 5.2...

Maybe this PR should go to master then or be changed to preserve in 5.1 too? Let's leave it up to Taylor to decide

@taylorotwell
Copy link
Member

Going to hold off on this.

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