Skip to content

Commit

Permalink
New Random operation
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 22, 2020
1 parent 8374728 commit e20bd8e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use loophp\collection\Operation\Pluck;
use loophp\collection\Operation\Prepend;
use loophp\collection\Operation\Product;
use loophp\collection\Operation\Random;
use loophp\collection\Operation\Range;
use loophp\collection\Operation\Reduction;
use loophp\collection\Operation\Reverse;
Expand Down Expand Up @@ -345,6 +346,11 @@ public function product(iterable ...$iterables): BaseInterface
return $this->run(new Product(...$iterables));
}

public function random(int $size = 1): BaseInterface
{
return $this->run(new Random($size));
}

public static function range(float $start = 0.0, float $end = INF, float $step = 1.0): BaseInterface
{
return (new self())->run(new Range($start, $end, $step));
Expand Down
2 changes: 2 additions & 0 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use loophp\collection\Contract\Operation\Pluckable;
use loophp\collection\Contract\Operation\Prependable;
use loophp\collection\Contract\Operation\Productable;
use loophp\collection\Contract\Operation\Randomable;
use loophp\collection\Contract\Operation\Rangeable;
use loophp\collection\Contract\Operation\Reductionable;
use loophp\collection\Contract\Operation\Reverseable;
Expand Down Expand Up @@ -115,6 +116,7 @@ interface Collection extends
Pluckable,
Prependable,
Productable,
Randomable,
Rangeable,
Reduceable,
Reductionable,
Expand Down
15 changes: 15 additions & 0 deletions src/Contract/Operation/Randomable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Contract\Operation;

use loophp\collection\Contract\Base;

interface Randomable
{
/**
* @return \loophp\collection\Base|\loophp\collection\Contract\Collection
*/
public function random(int $size = 1): Base;
}
27 changes: 27 additions & 0 deletions src/Operation/Random.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace loophp\collection\Operation;

use Closure;
use Generator;
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\Run;

final class Random extends AbstractOperation implements Operation
{
public function __construct(int $size = 1)
{
$this->storage = [
'size' => $size,
];
}

public function __invoke(): Closure
{
return static function (iterable $collection, int $size): Generator {
yield from (new Run(new Limit($size)))((new Run(new Shuffle()))($collection));
};
}
}

0 comments on commit e20bd8e

Please sign in to comment.