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

[7.x] Add until Collection method #32262

Merged
merged 2 commits into from
Apr 8, 2020

Conversation

jasonmccreary
Copy link
Contributor

This introduces a new until Collection method. It is similar to the take_while method in Ruby.

While this functionality may be achieved using combinations of the existing Collection methods or with custom logic, the code clarity provided by this single method brings value.

For example:

[$before, $after] = $primes->partition(function ($item) {
    return $item < 11;
});
$before->dump();

May simply be written as:

$primes->until(11)->dump();

This would be useful as a higher order method as well.

@jasonmccreary jasonmccreary force-pushed the until-collection-method branch from 8f859f3 to 127c916 Compare April 6, 2020 18:40
@jasonmccreary
Copy link
Contributor Author

jasonmccreary commented Apr 6, 2020

If accepted, I'd be glad to follow-up with the Higher Order Proxy for until as well as the update to the Docs.

@taylorotwell
Copy link
Member

Curious to see some application examples of when this might be helpful in a typical Laravel app...

@jasonmccreary
Copy link
Contributor Author

I needed to get a subset of Orders between a certain date and I had a sorted Eloquent collection of them.

filter and other methods could work, but felt something like until got it in a single go and was more expressive.

Chatted with @adamwathan and @reinink about it. They both hand macros that did something similar, so I figured I'd try to PR it.

@taylorotwell
Copy link
Member

Interesting. Can you share that snippet of code?

@GrahamCampbell GrahamCampbell changed the title Add until Collection method [7.x] Add until Collection method Apr 6, 2020
@GrahamCampbell
Copy link
Member

We probably want this on lazy collections too?

@jasonmccreary
Copy link
Contributor Author

jasonmccreary commented Apr 6, 2020

@taylorotwell, unfortunately, it was just a quick script to rebuild some lost order data. So I didn't keep it. However, I ended up doing a foreach just because finding the right combination of filter or short-circuiting each felt more verbose.

Glad to sit on it until I come across another need.

@GrahamCampbell, yeah, I think it'd be pretty nice as a Higher Order Proxy too. That way it's easier to pass custom logic.

$prs->until->unapproved();

Glad to add it to whatever. Just want to make sure it's merge-worthy first.

@taylorotwell
Copy link
Member

I'm pretty good with it. Do you think there is merit to naming it takeWhile just so it has parity with the existing Ruby method people might already be familiar with?

@jasonmccreary
Copy link
Contributor Author

jasonmccreary commented Apr 7, 2020

Your call. I only changed it to have the nice single word method name.

Also, this stops when the condition is true. Whereas takeWhile might imply different logic, e.g. go while condition is true.

@taylorotwell
Copy link
Member

OK, that's fine. Let's go ahead and wrap this up then with the HigherOrder support and LazyCollection support.

@jasonmccreary jasonmccreary force-pushed the until-collection-method branch from 1bc80d4 to 54f2137 Compare April 7, 2020 17:02
@taylorotwell
Copy link
Member

@jasonmccreary ping me when you are done with 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