Skip to content
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

Closed
wants to merge 6 commits into from
Closed

feat: add plus operation #314

wants to merge 6 commits into from

Conversation

drupol
Copy link
Collaborator

@drupol drupol commented Nov 11, 2023

This should fix #313

This PR:

  • Fix ...
  • Provide ...
  • It breaks backward compatibility
  • Is covered by unit tests
  • Has static analysis tests (psalm, phpstan)
  • Has documentation
  • Is an experimental thing

Follows #. Related to #. Fixes #.

Copy link

what-the-diff bot commented Nov 11, 2023

PR Summary

  • Introduction of a new file Plus.php
    Added a new file named Plus.php which contains a class known as Plus. This class is part of the Operation namespace and will likely contribute to the mathematical functions of the application.

@drupol drupol mentioned this pull request Nov 11, 2023
@drupol drupol force-pushed the feat/add-plus-operation branch from 2791ce0 to 177574e Compare November 11, 2023 16:30
@drupol drupol force-pushed the feat/add-plus-operation branch from 177574e to fa73667 Compare November 11, 2023 16:32
*
* @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]));
Copy link
Contributor

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);
}

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

@github-actions github-actions bot added the stale label Nov 19, 2023
@loophp loophp deleted a comment from github-actions bot Nov 19, 2023
@drupol drupol removed the stale label Nov 19, 2023
@drupol
Copy link
Collaborator Author

drupol commented Nov 25, 2023

@rela589n Are you willing to contribute to the documentation for this operation?

Copy link

Since this pull request has not had any activity within the last 5 days, I have marked it as stale.
I will close it if no further activity occurs within the next 5 days.

@github-actions github-actions bot added the stale label Nov 30, 2023
@drupol
Copy link
Collaborator Author

drupol commented Dec 1, 2023

No more feedback from the reporter, closing this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plus operation RFC
2 participants