-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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] Add reduce with keys to collections and lazy collections #35839
Conversation
Thank you @taylorotwell |
The naming here is quite unfortunate 😢 The With this new I would've much preferred not to even have this separate method. When do you ever need the initial value?? Would be much better to simply make this a breaking change in the next release, and have the regular |
I think initial is passed at the same level with the closure, it's not passed to the closure. What do you say we just add |
Oh, I hadn't even checked, and took your word for it. I checked now, and So of course, we should just add it to BTW, this method can go into |
One difference I see between some |
Yes. You can always just iterate over |
This adds the method
reduceWithKeys
toCollection
s andLazyCollection
s.Similar to
map
andmapWithKeys
, this augmentsreduce
to pass associative arrays' keys to its callback. We can't do that currently because php'sarray_reduce
doesn't pass array keys to its callback.So, you can do this:
I tried to mirror what's already been done with
map
,mapWithKeys
,reduce
, and respective tests.