-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Product operation to compute the cartesian product.
- Loading branch information
Showing
7 changed files
with
132 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 |
---|---|---|
|
@@ -82,6 +82,9 @@ pluck | |
prepend | ||
------- | ||
|
||
product | ||
------- | ||
|
||
rebase | ||
------ | ||
|
||
|
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
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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract; | ||
|
||
/** | ||
* Interface Productable. | ||
*/ | ||
interface Productable | ||
{ | ||
/** | ||
* Compute the cartesian product. | ||
* | ||
* @param iterable ...$iterables | ||
* | ||
* @return \loophp\collection\Contract\Collection<mixed> | ||
*/ | ||
public function product(iterable ...$iterables): Base; | ||
} |
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,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use Closure; | ||
use Generator; | ||
use loophp\collection\Contract\Operation; | ||
use loophp\collection\Iterator\IterableIterator; | ||
|
||
use function count; | ||
|
||
/** | ||
* Class Product. | ||
*/ | ||
final class Product implements Operation | ||
{ | ||
/** | ||
* @var iterable[] | ||
*/ | ||
private $iterables; | ||
|
||
/** | ||
* Product constructor. | ||
* | ||
* @param iterable ...$iterables | ||
*/ | ||
public function __construct(iterable ...$iterables) | ||
{ | ||
$this->iterables = $iterables; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function on(iterable $collection): Closure | ||
{ | ||
$iterables = $this->iterables; | ||
|
||
$cartesian = function (array $input): Generator { | ||
return $this->cartesian($input); | ||
}; | ||
|
||
return static function () use ($iterables, $collection, $cartesian): Generator { | ||
$its = [$collection]; | ||
|
||
foreach ($iterables as $iterable) { | ||
$its[] = new IterableIterator($iterable); | ||
} | ||
|
||
yield from $cartesian($its); | ||
}; | ||
} | ||
|
||
/** | ||
* @param array<iterable> $iterators | ||
* | ||
* @return Generator<array> | ||
*/ | ||
private function cartesian(array $iterators): Generator | ||
{ | ||
$last = array_pop($iterators); | ||
|
||
if (null === $last) { | ||
yield []; | ||
|
||
return; | ||
} | ||
|
||
foreach ($this->cartesian($iterators) as $item) { | ||
foreach ($last as $value) { | ||
yield $item + [count($item) => $value]; | ||
} | ||
} | ||
} | ||
} |