-
-
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa73667
feat: add `plus` operation
drupol 9f29100
add tests
drupol 1026d49
fix: add missing implementation
drupol 7533c47
update implementation
drupol c633b7e
fix: phpstan baseline update
drupol 90943dd
fix: variable name
drupol File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract\Operation; | ||
|
||
use loophp\collection\Contract\Collection; | ||
|
||
/** | ||
* @template TKey | ||
* @template T | ||
*/ | ||
interface Plusable | ||
{ | ||
/** | ||
* TODO - Plus items. | ||
* | ||
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#plus | ||
* | ||
* @template UKey | ||
* @template U | ||
* | ||
* @param iterable<UKey, U> $items | ||
* | ||
* @return Collection<int|TKey|UKey, T|U> | ||
*/ | ||
public function plus(iterable $items): Collection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use Closure; | ||
use Generator; | ||
use loophp\iterators\ConcatIterableAggregate; | ||
|
||
/** | ||
* @immutable | ||
* | ||
* @template TKey | ||
* @template T | ||
*/ | ||
final class Plus extends AbstractOperation | ||
{ | ||
/** | ||
* @template UKey | ||
* @template U | ||
* | ||
* @return Closure(iterable<UKey, U>): Closure(iterable<TKey, T>): iterable<int|TKey|UKey, T|U> | ||
*/ | ||
public function __invoke(): Closure | ||
{ | ||
$comparatorCallback = | ||
/** | ||
* @param T $left | ||
* | ||
* @return Closure(T): bool | ||
*/ | ||
static fn (mixed $left): Closure => | ||
/** | ||
* @param T $right | ||
*/ | ||
static fn (mixed $right): bool => $left === $right; | ||
|
||
return | ||
/** | ||
* @param iterable<UKey, U> $items | ||
* | ||
* @return Closure(iterable<TKey, T>): iterable<int|TKey|UKey, T|U> | ||
*/ | ||
static fn (iterable $items): Closure => | ||
/** | ||
* @param iterable<TKey, T> $iterable | ||
* | ||
* @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])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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):
Moreover, it would be really pity if
plus([])
would drop all key duplicates. The following test fails as well: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.