Repository navigation
[Sequence] Add "terminal" methods: element access #29
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e97c75f
Add sequence element access terminals
nikophil 086106a
Reject negative indexes in elementAt() and elementAtOrNull()
nikophil baff5a2
Implement singleOrNull() directly instead of catching single()
nikophil 390efe1
Restrict usage of trait IterableTerminalsLogic
nikophil d0cff5d
Remove @phpstan-require-implements on IterableTerminalsLogic
nikophil 5382540
Drop the Sequence import left unused on IterableTerminalsLogic
nikophil f9e300e
Align #[NoDiscard] on Sequence with the Collection rule
nikophil 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 hidden or 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 hidden or 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,36 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of the Noctud Collection. | ||
| * Copyright (c) Noctud.dev | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Noctud\Collection\Exception; | ||
|
|
||
| use Noctud\Collection\Collection; | ||
| use Noctud\Collection\Sequence\Sequence; | ||
|
|
||
| /** | ||
| * Names what a message is about, so that a body shared by the eager and the lazy side does not | ||
| * have to carry two versions of its own error text: a sequence must not be told a "Collection" is | ||
| * empty, and that difference is the only thing that used to keep those bodies apart. | ||
| * | ||
| * @internal | ||
| */ | ||
| trait NamesItsSubject | ||
| { | ||
| /** | ||
| * @param Collection<mixed>|Sequence<mixed> $subject | ||
| */ | ||
| private static function subjectName(Collection|Sequence $subject): string | ||
| { | ||
| // The parameter type makes this exhaustive, so the default arm *is* the Collection case | ||
| // rather than a catch-all. Widening that union means adding an arm here. | ||
| return match (true) { | ||
| $subject instanceof Sequence => 'Sequence', | ||
| default => 'Collection', | ||
| }; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,100 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of the Noctud Collection. | ||
| * Copyright (c) Noctud.dev | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Noctud\Collection; | ||
|
|
||
| use Closure; | ||
| use Noctud\Collection\Exception\NoSuchElementException; | ||
|
|
||
| /** | ||
| * Terminal operations implemented by walking $this and nothing else, shared by the eager | ||
| * Collection side and the lazy Sequence side. | ||
| * | ||
| * A body belongs here only if it is identical for both, which excludes the family the eager side | ||
| * answers from its store in O(1) (first/last/contains/count). A message naming the subject is no | ||
| * longer a reason to split: the exception derives that noun from $this. | ||
| * | ||
| * Consumers declare the contract, so the PHPDoc here is {@inheritDoc}: it resolves against | ||
| * Collection<E> or Sequence<E> depending on who uses the trait. | ||
| * | ||
| * @template E | ||
|
nikophil marked this conversation as resolved.
|
||
| * | ||
| * @internal | ||
| */ | ||
| trait IterableTerminalsLogic | ||
|
nikophil marked this conversation as resolved.
|
||
| { | ||
| /** {@inheritDoc} */ | ||
| public function single() | ||
| { | ||
| $found = false; | ||
| $result = null; | ||
|
|
||
| foreach ($this as $v) { | ||
| if ($found) { | ||
| throw NoSuchElementException::subjectHasMoreThanOneElement($this); | ||
| } | ||
|
|
||
| $result = $v; | ||
| $found = true; | ||
| } | ||
|
|
||
| if (!$found) { | ||
| throw NoSuchElementException::emptySubject($this); | ||
| } | ||
|
|
||
| return $result; // @phpstan-ignore return.type | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * Not a try-catch around single(): iterating $this runs user closures on the lazy side, and a | ||
| * NoSuchElementException raised inside one is a real error, not an answer to this question. | ||
| */ | ||
| public function singleOrNull(): mixed | ||
| { | ||
| $found = false; | ||
| $result = null; | ||
|
|
||
| foreach ($this as $v) { | ||
| if ($found) { | ||
| return null; | ||
| } | ||
|
|
||
| $result = $v; | ||
| $found = true; | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| public function find(Closure $predicate): mixed | ||
| { | ||
| foreach ($this as $i => $v) { | ||
| if ($predicate($v, $i)) { | ||
| return $v; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** {@inheritDoc} */ | ||
| public function expect(Closure $predicate) | ||
| { | ||
| foreach ($this as $i => $v) { | ||
| if ($predicate($v, $i)) { | ||
| return $v; | ||
| } | ||
| } | ||
|
|
||
| throw new NoSuchElementException('No element matching the predicate was found'); | ||
| } | ||
| } | ||
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.