-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
fix: allow inner loop traversals of SimpleCachingIteratorAggregate #50
Conversation
PR Summary
|
Thanks for your pull request, I will review it carefully as soon as I have a bit of time. |
I've updated a bit the PR, do you think we could do something to avoid the |
I found a case where this PR would not be valid, here it is: <?php
declare(strict_types=1);
namespace Issue49;
use Generator;
use loophp\iterators\SimpleCachingIteratorAggregate;
require __DIR__ . '/vendor/autoload.php';
$input = static function (): Generator {
yield 'a' => 1;
yield 'a' => 2;
yield 'a' => 3;
yield 'a' => 4;
yield 'a' => 5;
yield 'a' => 6;
};
$iter = new SimpleCachingIteratorAggregate($input());
foreach ($iter as $ko => $vo) {
if ($ko === 2) {
foreach ($iter as $ki => $vi) {
var_dump("inner", $ki, $vi);
}
}
var_dump("outer", $ko, $vo);
} The issue lies in the fact that we rely on the value of the key. Since there can be multiple same keys in an iterator, we need to find another way to achieve this if you want this PR to be merged. |
Since this pull request has not had any activity within the last 5 days, I have marked it as stale. |
…plicated key values
I'm not sure what is the expected behavior thereon. I have just added These two statements accurately represent it: self::assertSame([
['a', 1],
['a', 2],
['a', 3],
], $firstIterationItems);
self::assertSame([
['a', 3],
], $secondIterationItems); That being said, current logic of inner loop iterator traversal, looks expected to me: public function testInnerTraversalConsumesDuplicatedKeyValues(): void
{
$input = static function (): Generator {
yield 'a' => 1;
yield 'a' => 2;
yield 'a' => 3;
yield 'b' => 4;
yield 'b' => 5;
};
$iter = new SimpleCachingIteratorAggregate($input());
$outerItems = $innerItems = [];
foreach ($iter as $ko => $vo) {
if ($vo === 2) {
foreach ($iter as $ki => $vi) {
$innerItems[] = [$ki, $vi];
}
}
$outerItems[] = [$ko, $vo];
}
self::assertSame([
['a', 1],
['a', 2],
['b', 5],
], $outerItems);
self::assertSame([
['a', 2],
['a', 3],
['b', 4],
['b', 5],
], $innerItems);
} Please, let me know what you think on this matter. Thank you! |
Since this pull request has not had any activity within the last 5 days, I have marked it as stale. |
Hi there! Just in case you don't like the idea behind suggested changes or the implementation, please let me know so that current PR may be closed. Thank you |
Hi ! I haven't had the time to review it yet... This weekend with the Fosdem it's not going to be possible. Planning to do it next week hopefully! I'll keep you posted, no worries. Cheers. |
Since this pull request has not had any activity within the last 5 days, I have marked it as stale. |
This PR adds consecutive caching to
SimpleCachingIteratorAggregate
allowing to traverse the same iterator within the loop without affecting outer iteration.See #49