-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat: Add Chain::merge functionality (Close #54) #55
Conversation
First, thanks for your contribution. You implementation does three things:
In #54 you describe the following scenario: Chain::create(array_merge($object->getOutsidePhotos(), $object->getInsidePhotos()))
->map(fn (Photo $photo) => $photo->getUrl())
->map(fn (string $url) => $this....); And propose the following solution: Chain::merge($object->getOutsidePhotos(), $object->getInsidePhotos())
->map(fn (Photo $photo) => $photo->getUrl())
->map(fn (string $url) => $this....); But following the philosophy of Chain I think the correct approach to implement this would be: Chain::create($object->getOutsidePhotos())
->merge($object->getInsidePhotos())
->map(fn (Photo $photo) => $photo->getUrl())
->map(fn (string $url) => $this....); This already works and I don't think that having a separate static method to create to create a Chain from multiple arrays is not very useful and in fact, works against the philosophy of this library. But the other two features from your PR would be useful. I would appreciate it if you would update the PR to implement the faster merging in the |
Thank you for the detailed explanation and the overview of my implementation. I will modify it but there is a slight concern, which is the difference of the two merging methods.
Function implemented would always overwrite previous values. Also it will not renumber in any way. These changes might introduce bugs in the client's applications. Example with array_merge: $array1 = array();
$array2 = array(1 => "data");
$result = array_merge($array1, $array2); Result:
Example with PR function: Chain::create([])
->merge([1 => 'data']) Result:
Should I still implement it? |
@colorninja You're right, this would break compatibility. We could add an option (defaults to |
I think a separate trait would be better. The boolean would be too subtle, where a different trait will actually provoke questions upon IDE autocomplete (wondering what the difference is). I am having a hard time thinking of a intuitive name of the trait/method. Any ideas? |
@florianeckerstorfer What about |
@colorninja Yes, either |
Turns out that PHP automatically optimizes passing by reference, so no need for the |
This should be a bit more efficient than the internal array_merge. It is using references in order to improve speed when using large arrays. It also does not modify passed arrays to avoid unexpected issues for consumers.