-
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
[5.4] Create a mapToGroups method #18949
Conversation
JosephSilber
commented
Apr 26, 2017
•
edited
Loading
edited
4312073
to
503d585
Compare
Seems neat. I have, on occasion, needed to use Also, the above example with Higher Order Messages can be written as: $users->groupBy("country")->map->pluck("id"); Want to see something even crazier? Add a groupBy proxy and you can write it like this: $users->groupBy->country->map->map->map->id; Aside: |
I intentionally created a simple example. But if you do more complex stuff, you may end up with two closures. Additionally, if the value you're mapping to requires the original key, you're out of luck without this |
Yeah, I figured as much. Was just pointing it out.
Yeah. What I usually try to do is map first and include an extra entry for grouping if needed. But I always felt that it was a bit of a hack. Just depends on use case. |
I feel like this should follow the naming of |
@ConnorVG to me, $collection = collect([
'a' => [2, 5],
'b' => [8, 9],
]);
$collection->groupMap(function ($number) {
return $number * 2;
});
/*
[
'a' => [4, 10],
'b' => [16, 18],
]
*/ ...which would also maybe be a useful method, but this |
Eh? That's just a map map. |
I think we could add another test for numeric keys: $users->mapToGroups(function ($user) {
return [$user->age => $user->id];
});
/*
[
17 => [23, 1],
23 => [34, 3],
31 => [9],
44 => [4, 7, 2],
]
*/ So we assert that it won't reindex the array in the future... |
503d585
to
1a36e74
Compare