groupBy recurses for each value added to a group
gzumba opened this issue · comments
The implementation of groupBy causes issues (recursion depth grows huge) when a lot of entries resolve to the same group as the group is built using a lazy collection.
Easy to demonstrate with eg (enable xdebug with recursion limit to see the issue):
$res = Collection::from(range(1, 300))
->groupBy(function ($val) {return 1;})
->map('DusanKasan\Knapsack\toArray')
->toArray()
;
Quite simple to fix by changing the groupBy function to use an array instead of Collection to build the groups.
I see, thank you for filing this issue @gzumba. I agree with your proposed solution of replacing the Collection inside the groupBy function with array.
Considering the fix is quite easy and it won't affect documentation or tests, would you like to submit a PR for this?
Sure, I will do a PR first thing tomorrow.
Resolved by #26