-
-
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
Feature: Add Squash operation #83
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6e8f172
feat: Add Squash operation.
drupol fca4460
Remove $throwOnException parameter.
drupol d025eab
Add tests.
drupol 93f86a7
Add Interfaces.
drupol 85899d1
Simplify things - there is no need to have an Operation for this.
drupol cade381
Simplify even more.
drupol b9f152c
Remove unused use statement.
drupol b188f0b
Add documentation.
drupol 34020ae
Fix missing include in documentation.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
use Exception; | ||
use loophp\collection\Collection; | ||
|
||
include __DIR__ . '/../../../../vendor/autoload.php'; | ||
|
||
$results = Collection::fromIterable(range(0, 100)) | ||
->filter(static fn (int $int) => 0 === $int % 2) | ||
->map(static fn (int $int) => 'document' . $int . '.pdf') | ||
->map( | ||
static function (string $doc): bool { | ||
if (false === file_exists('/doc/' . $doc)) { | ||
throw new Exception('Unexistent file'); | ||
} | ||
|
||
return file_get_contents($doc); | ||
} | ||
) | ||
->squash(); // Instantly trigger an exception if a file does not exist. | ||
|
||
// If no exception, you can continue the processing... | ||
$results = $results | ||
->filter( | ||
static function (string $document): bool { | ||
return false !== strpos($document, 'foobar'); | ||
} | ||
); |
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,25 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract\Operation; | ||
|
||
use loophp\collection\Contract\Collection; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
*/ | ||
interface Squashable | ||
{ | ||
/** | ||
* @psalm-return \loophp\collection\Collection<TKey, T> | ||
*/ | ||
public function squash(): Collection; | ||
} |
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.
If you need I can help adding the docs for this method, but would just like to understand a bit better how this works. I'm looking at the docs for
pack
andunpack
but still don't get why they're needed here 😓.If you don't mind can you explain what's the difference between this and doing the below?
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 suspect it has to do with the collections allowing keys that are not only
int|string
as well as duplicate keys, right?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.
Yes exactly.
iterator_to_array()
will fail if keys are different fromint|string
.See: https://3v4l.org/FW0Ca