-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
221 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
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 Bonami\Collection\Monoid; | ||
|
||
/** @phpstan-implements Monoid<double> */ | ||
class DoubleProductMonoid implements Monoid | ||
{ | ||
|
||
public function concat($a, $b) | ||
{ | ||
return $a * $b; | ||
} | ||
|
||
public function getEmpty() | ||
{ | ||
return 1; | ||
} | ||
} |
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 Bonami\Collection\Monoid; | ||
|
||
/** @phpstan-implements Monoid<double> */ | ||
class DoubleSumMonoid implements Monoid | ||
{ | ||
|
||
public function concat($a, $b) | ||
{ | ||
return $a + $b; | ||
} | ||
|
||
public function getEmpty() | ||
{ | ||
return 0; | ||
} | ||
} |
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 Bonami\Collection\Monoid; | ||
|
||
/** @phpstan-implements Monoid<int> */ | ||
class IntProductMonoid implements Monoid | ||
{ | ||
|
||
public function concat($a, $b) | ||
{ | ||
return $a * $b; | ||
} | ||
|
||
public function getEmpty() | ||
{ | ||
return 1; | ||
} | ||
} |
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 Bonami\Collection\Monoid; | ||
|
||
/** @phpstan-implements Monoid<int> */ | ||
class IntSumMonoid implements Monoid | ||
{ | ||
|
||
public function concat($a, $b) | ||
{ | ||
return $a + $b; | ||
} | ||
|
||
public function getEmpty() | ||
{ | ||
return 0; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bonami\Collection\Monoid; | ||
|
||
/** @phpstan-template A */ | ||
interface Monoid | ||
{ | ||
|
||
/** | ||
* A binary operation. | ||
* | ||
* It must follow monoid laws: | ||
* - associativity - `concat(concat($a, $b), $c) === concat($a, concat($b, $c))` for any element `A` | ||
* - identity law - `concat($a, getEmpty()) === concat(getEmpty(), $a) === $a` | ||
* | ||
* @phpstan-param A $a | ||
* @phpstan-param A $b | ||
* | ||
* @phpstan-return A | ||
*/ | ||
public function concat($a, $b); | ||
|
||
/** | ||
* Neutral element for binary concat operation | ||
* | ||
* @phpstan-return A | ||
*/ | ||
public function getEmpty(); | ||
} |
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 Bonami\Collection\Monoid; | ||
|
||
/** @phpstan-implements Monoid<string> */ | ||
class StringMonoid implements Monoid | ||
{ | ||
|
||
public function concat($a, $b) | ||
{ | ||
return $a . $b; | ||
} | ||
|
||
public function getEmpty() | ||
{ | ||
return ""; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bonami\Collection\Monoid; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class MonoidTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider provideFixtures | ||
* | ||
* @phpstan-template A | ||
* | ||
* @phpstan-param A $a | ||
* @phpstan-param A $b | ||
* @phpstan-param Monoid<A> $monoid | ||
* @phpstan-param A $result | ||
*/ | ||
public function testFromMonoids($a, $b, Monoid $monoid, $result): void | ||
{ | ||
self::assertSame($a, $monoid->concat($a, $monoid->getEmpty())); | ||
self::assertSame($a, $monoid->concat($monoid->getEmpty(), $a)); | ||
self::assertSame($result, $monoid->concat($a, $b)); | ||
} | ||
|
||
/** @phpstan-return iterable<array{0: mixed, 1: mixed, 2: Monoid<mixed>, 3: mixed}> */ | ||
public function provideFixtures(): iterable | ||
{ | ||
yield [1, 2, new IntSumMonoid(), 3]; | ||
yield [1, 2, new IntProductMonoid(), 2]; | ||
yield [1.1, 2.1, new DoubleSumMonoid(), 3.2]; | ||
yield [1.1, 2.1, new DoubleProductMonoid(), 2.31]; | ||
yield ["foo", "bar", new StringMonoid(), "foobar"]; | ||
} | ||
} |