-
-
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 8ae1a57
Showing
6 changed files
with
122 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,52 @@ | ||
<?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; | ||
|
||
/** | ||
* @return array | ||
* @no-named-arguments | ||
*/ | ||
function cartesian_product(string $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); | ||
$aggregation[] = "{$l}{$separator}{$r}"; | ||
++$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