Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
MapIterableAggregate
and FilterIterableAggregate
.
- Loading branch information
Showing
5 changed files
with
218 additions
and
0 deletions.
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,55 @@ | ||
<?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\iterators; | ||
|
||
use Closure; | ||
use Generator; | ||
use IteratorAggregate; | ||
|
||
/** | ||
* @template TKey | ||
* @template T | ||
* | ||
* @implements IteratorAggregate<TKey, T> | ||
*/ | ||
final class FilterIterableAggregate implements IteratorAggregate | ||
{ | ||
/** | ||
* @var iterable<TKey, T> | ||
*/ | ||
private iterable $iterable; | ||
|
||
/** | ||
* @var Closure(T, TKey, iterable<TKey, T>): bool | ||
*/ | ||
private Closure $predicate; | ||
|
||
/** | ||
* @param iterable<TKey, T> $iterable | ||
* @param (Closure(T, TKey, iterable<TKey, T>): bool) $predicate | ||
*/ | ||
public function __construct(iterable $iterable, Closure $predicate) | ||
{ | ||
$this->iterable = $iterable; | ||
$this->predicate = $predicate; | ||
} | ||
|
||
/** | ||
* @return Generator<TKey, T> | ||
*/ | ||
public function getIterator(): Generator | ||
{ | ||
foreach ($this->iterable as $key => $value) { | ||
if (($this->predicate)($value, $key, $this->iterable)) { | ||
yield $key => $value; | ||
} | ||
} | ||
} | ||
} |
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,54 @@ | ||
<?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\iterators; | ||
|
||
use Closure; | ||
use Generator; | ||
use IteratorAggregate; | ||
|
||
/** | ||
* @template TKey | ||
* @template T | ||
* @template W | ||
* | ||
* @implements IteratorAggregate<TKey, W> | ||
*/ | ||
final class MapIterableAggregate implements IteratorAggregate | ||
{ | ||
/** | ||
* @var Closure(T, TKey, iterable<TKey, T>): W | ||
*/ | ||
private Closure $closure; | ||
|
||
/** | ||
* @var iterable<TKey, T> | ||
*/ | ||
private iterable $iterable; | ||
|
||
/** | ||
* @param iterable<TKey, T> $iterable | ||
* @param (Closure(T, TKey, iterable<TKey, T>): W) $closure | ||
*/ | ||
public function __construct(iterable $iterable, Closure $closure) | ||
{ | ||
$this->iterable = $iterable; | ||
$this->closure = $closure; | ||
} | ||
|
||
/** | ||
* @return Generator<TKey, W> | ||
*/ | ||
public function getIterator(): Generator | ||
{ | ||
foreach ($this->iterable as $key => $value) { | ||
yield $key => ($this->closure)($value, $key, $this->iterable); | ||
} | ||
} | ||
} |
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 tests\loophp\iterators; | ||
|
||
use loophp\iterators\FilterIterableAggregate; | ||
use PHPUnit\Framework\TestCase; | ||
use function count; | ||
|
||
/** | ||
* @internal | ||
* @coversDefaultClass \loophp\iterators | ||
*/ | ||
final class FilterIterableAggregateTest extends TestCase | ||
{ | ||
public function testBasic(): void | ||
{ | ||
$iterator = (new FilterIterableAggregate( | ||
range(0, 5), | ||
static fn (int $v, int $key, iterable $iterable): bool => 0 === (($v + 2 * $key + count($iterable)) % 2) | ||
)); | ||
|
||
$expected = [ | ||
0 => 0, | ||
2 => 2, | ||
4 => 4, | ||
]; | ||
|
||
self::assertSame($expected, iterator_to_array($iterator)); | ||
} | ||
} |
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 tests\loophp\iterators; | ||
|
||
use loophp\iterators\MapIterableAggregate; | ||
use PHPUnit\Framework\TestCase; | ||
use function gettype; | ||
|
||
/** | ||
* @internal | ||
* @coversDefaultClass \loophp\iterators | ||
*/ | ||
final class MapIterableAggregateTest extends TestCase | ||
{ | ||
public function testBasic(): void | ||
{ | ||
$iterator = (new MapIterableAggregate( | ||
range('a', 'c'), | ||
static fn (string $letter, int $key, iterable $iterable): string => sprintf('%s::%s::%s', $key, $letter, gettype($iterable)) | ||
)); | ||
|
||
$expected = [ | ||
'0::a::array', | ||
'1::b::array', | ||
'2::c::array', | ||
]; | ||
|
||
self::assertSame($expected, iterator_to_array($iterator)); | ||
} | ||
} |