Skip to content

Commit

Permalink
refactor: Introduce MultipleIterableIterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 20, 2020
1 parent 4390145 commit aa7701d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
33 changes: 33 additions & 0 deletions src/Iterator/MultipleIterableIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Iterator;

use AppendIterator;
use NoRewindIterator;

/**
* @internal
*
* @psalm-template TKey
* @psalm-template T
*
* @extends ProxyIterator<TKey, T>
*/
final class MultipleIterableIterator extends ProxyIterator
{
/**
* @psalm-param iterable<TKey, T> $iterable
*/
public function __construct(iterable ...$iterators)
{
$appendIterator = new AppendIterator();

foreach ($iterators as $iterator) {
$appendIterator->append(new NoRewindIterator(new IterableIterator($iterator)));
}

$this->iterator = $appendIterator;
}
}
7 changes: 3 additions & 4 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Iterator\MultipleIterableIterator;

/**
* @psalm-template TKey
Expand All @@ -32,10 +33,8 @@ public function __invoke(): Closure
*
* @psalm-return Generator<int|TKey, T>
*/
static function (Iterator $iterator) use ($items): Generator {
yield from $iterator;

return yield from $items;
static function (Iterator $iterator) use ($items): Iterator {
return new MultipleIterableIterator($iterator, $items);
};
}
}
9 changes: 3 additions & 6 deletions src/Operation/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Iterator\MultipleIterableIterator;

/**
* @psalm-template TKey
Expand All @@ -30,12 +31,8 @@ public function __invoke(): Closure
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($sources): Generator {
yield from $iterator;

foreach ($sources as $source) {
yield from $source;
}
static function (Iterator $iterator) use ($sources): Iterator {
return new MultipleIterableIterator($iterator, ...$sources);
};
}
}
7 changes: 3 additions & 4 deletions src/Operation/Prepend.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Generator;
use Iterator;
use loophp\collection\Iterator\MultipleIterableIterator;

/**
* @psalm-template TKey
Expand All @@ -32,10 +33,8 @@ public function __invoke(): Closure
*
* @psalm-return Generator<int|TKey, T>
*/
static function (Iterator $iterator) use ($items): Generator {
yield from $items;

return yield from $iterator;
static function (Iterator $iterator) use ($items): Iterator {
return new MultipleIterableIterator($items, $iterator);
};
}
}

0 comments on commit aa7701d

Please sign in to comment.