-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
cartesian_product
and InvalidArgumentException::assertStringable
- Loading branch information
1 parent
5f5ddb6
commit 0216a5f
Showing
6 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/** | ||
* @package Functional-php | ||
* @author Hugo Sales <[email protected]> | ||
* @copyright 2021 Lars Strojny | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/lstrojny/functional-php | ||
*/ | ||
|
||
namespace Functional; | ||
|
||
use Functional\Exceptions\InvalidArgumentException; | ||
use Traversable; | ||
|
||
/** | ||
* @param string|array $separator | ||
* @param array ...$collections | ||
* @return array | ||
* @no-named-arguments | ||
*/ | ||
function cartesian_product($separator, ...$collections) | ||
{ | ||
InvalidArgumentException::assertIntegerGreaterThanOrEqual(\count($collections), 2, __FUNCTION__, 2); // TODO not great | ||
|
||
$aggregation = []; | ||
$left = \array_shift($collections); | ||
$index = 2; | ||
InvalidArgumentException::assertCollection($left, __FUNCTION__, $index); | ||
while (true) { | ||
$right = \array_shift($collections); | ||
InvalidArgumentException::assertCollection($right, __FUNCTION__, $index + 1); | ||
$left_index = 0; | ||
foreach ($left as $l) { | ||
$right_index = 0; | ||
foreach ($right as $r) { | ||
InvalidArgumentException::assertStringable($l, __FUNCTION__, $index, $left_index); | ||
InvalidArgumentException::assertStringable($r, __FUNCTION__, $index + 1, $right_index); | ||
if (\is_string($separator)) { | ||
$aggregation[] = "{$l}{$separator}{$r}"; | ||
} else if (\is_array($separator)) { | ||
foreach ($separator as $sep) { | ||
$aggregation[] = "{$l}{$sep}{$r}"; | ||
} | ||
} else { | ||
// TODO assert that $separator is string or array of strings | ||
} | ||
++$right_index; | ||
} | ||
++$left_index; | ||
} | ||
++$index; | ||
if (empty($collections)) { | ||
break; | ||
} else { | ||
$left = $aggregation; | ||
$aggregation = []; | ||
} | ||
} | ||
|
||
return $aggregation; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
/** | ||
* @package Functional-php | ||
* @author Hugo Sales <[email protected]> | ||
* @copyright 2021 Lars Strojny | ||
* @license https://opensource.org/licenses/MIT MIT | ||
* @link https://github.com/lstrojny/functional-php | ||
*/ | ||
|
||
namespace Functional\Tests; | ||
|
||
use function Functional\cartesian_product; | ||
|
||
class CartesianProductTest extends AbstractTestCase | ||
{ | ||
public function testCartesianProduct(): void | ||
{ | ||
self::assertSame(['1-one', '1-two', '2-one', '2-two'], cartesian_product('-', [1, 2], ['one', 'two'])); | ||
self::assertSame(['1one', '1two', '2one', '2two'], cartesian_product('', [1, 2], ['one', 'two'])); | ||
self::assertSame(['1-one-H', '1-one-He', '1-two-H', '1-two-He', '2-one-H', '2-one-He', '2-two-H', '2-two-He'], cartesian_product('-', [1, 2], ['one', 'two'], ['H', 'He'])); | ||
} | ||
} |
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