-
-
Notifications
You must be signed in to change notification settings - Fork 35
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 plus
operation
#314
Conversation
PR Summary
|
2791ce0
to
177574e
Compare
This should fix #313
177574e
to
fa73667
Compare
src/Operation/Plus.php
Outdated
* | ||
* @return Generator<int|TKey|UKey, T|U> | ||
*/ | ||
static fn (iterable $iterable): Generator => yield from (new Distinct())()($comparatorCallback)(static fn (mixed $value, mixed $key): mixed => $key)(new ConcatIterableAggregate([$iterable, $items])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@drupol , does it make any sense to remove duplicated indexes in the existing collection? I assume it would be more apparent if these were kept. Consider following test (fails with current implementation):
public function testPlusDoesNotRemoveExistingDuplicatedIndices(): void
{
$collection = Collection::fromIterable([
['id' => 1],
['id' => 2],
]);
$result = $collection
->unwrap()
->plus(['id' => 3, 'id2' => 4])
->wrap()
->all(false)
;
self::assertSame([
['id' => 1],
['id' => 2],
['id2' => 4],
], $result);
}
Moreover, it would be really pity if plus([])
would drop all key duplicates. The following test fails as well:
public function testPlusEmptyArrayDoesNotModifyCollection(): void
{
$collection = Collection::fromIterable([
['id' => 1],
['id' => 2],
]);
$result = $collection
->unwrap()
->plus([])
->wrap()
->all(false)
;
self::assertSame([
['id' => 1],
['id' => 2],
], $result);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice catch and test case. I will see how we can improve the implementation. Feel free to come up with ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the tests and the implementation, this is a bit uglier, but AFAIK there's no other alternative.
@rela589n Are you willing to contribute to the documentation for this operation? |
Since this pull request has not had any activity within the last 5 days, I have marked it as stale. |
No more feedback from the reporter, closing this PR. |
This should fix #313
This PR:
Follows #. Related to #. Fixes #.