Skip to content

[7.x] Add until Collection method#32262

Merged
taylorotwell merged 2 commits into
laravel:7.xfrom
jasonmccreary:until-collection-method
Apr 8, 2020
Merged

[7.x] Add until Collection method#32262
taylorotwell merged 2 commits into
laravel:7.xfrom
jasonmccreary:until-collection-method

Conversation

@jasonmccreary

Copy link
Copy Markdown
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

jasonmccreary commented Apr 6, 2020

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Member

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

@jasonmccreary

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator

We probably want this on lazy collections too?

@jasonmccreary

jasonmccreary commented Apr 6, 2020

Copy link
Copy Markdown
Contributor Author

@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
Copy Markdown
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

jasonmccreary commented Apr 7, 2020

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
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
Copy Markdown
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