Skip to content

Commit

Permalink
refactor: random operation: use RandomIterableAggregate.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Apr 6, 2022
1 parent 8afedbf commit 78e7a6f
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Operation/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

use Closure;
use Generator;
use loophp\iterators\LimitIterableAggregate;
use loophp\iterators\RandomIterableAggregate;

/**
* @immutable
*
* @template TKey
* @template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
*/
final class Random extends AbstractOperation
{
Expand All @@ -35,14 +39,16 @@ static function (int $seed): Closure {
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static function (int $size) use ($seed): Closure {
/** @var Closure(iterable<TKey, T>): Generator<TKey, T> $pipe */
$pipe = (new Pipe())()(
(new Shuffle())()($seed),
(new Limit())()($size)(0)
);

// Point free style.
return $pipe;
return
/**
* @param iterable<TKey, T> $iterable
*
* @return Generator<TKey, T>
*/
static function (iterable $iterable) use ($seed, $size): Generator {
// Point free style.
yield from new LimitIterableAggregate(new RandomIterableAggregate($iterable, $seed), 0, $size);
};
};
};
}
Expand Down

0 comments on commit 78e7a6f

Please sign in to comment.