-
Notifications
You must be signed in to change notification settings - Fork 8
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
5 changed files
with
102 additions
and
7 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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Marcosh\LamPHPda\Instances\ListL\ConcatenationMonoid; | ||
use Marcosh\LamPHPda\Instances\ListL\ListFoldable; | ||
use Marcosh\LamPHPda\ListL; | ||
use Marcosh\LamPHPda\Typeclass\Extra\ExtraFoldable; | ||
|
||
describe('ConcatenateListsWithFoldMap', function () { | ||
it('preserves the order when concatenating lists', function () { | ||
expect((new ExtraFoldable(new ListFoldable()))->fold( | ||
new ConcatenationMonoid(), | ||
new ListL([[1, 2], [3, 4], [5, 6]]) | ||
))->toBe([1, 2, 3, 4, 5, 6]); | ||
}); | ||
}); |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Marcosh\LamPHPda\Typeclass\Extra; | ||
|
||
use Marcosh\LamPHPda\Brand\Brand; | ||
use Marcosh\LamPHPda\HK\HK1; | ||
use Marcosh\LamPHPda\Typeclass\Foldable; | ||
use Marcosh\LamPHPda\Typeclass\Monoid; | ||
|
||
/** | ||
* @template T of Brand | ||
* | ||
* @psalm-immutable | ||
*/ | ||
final class ExtraFoldable | ||
{ | ||
/** | ||
* @param Foldable<T> $foldable | ||
*/ | ||
public function __construct(private readonly Foldable $foldable) | ||
{ | ||
} | ||
|
||
/** | ||
* @template A | ||
* @template M | ||
* @param Monoid<M> $mMonoid | ||
* @param callable(A): M $f | ||
* @param HK1<T, A> $hk | ||
* @return M | ||
*/ | ||
public function foldMap(Monoid $mMonoid, callable $f, $hk) | ||
{ | ||
return $this->foldable->foldr( | ||
/** | ||
* @param A $a | ||
* @param M $m | ||
* @return M | ||
*/ | ||
static fn ($a, $m) => $mMonoid->append($f($a), $m), | ||
$mMonoid->mempty(), | ||
$hk | ||
); | ||
} | ||
|
||
/** | ||
* @template M | ||
* @param Monoid<M> $mMonoid | ||
* @param HK1<T, M> $hk | ||
* @return M | ||
*/ | ||
public function fold(Monoid $mMonoid, $hk) | ||
{ | ||
return $this->foldMap( | ||
$mMonoid, | ||
/** | ||
* @param M $m | ||
* @return M | ||
*/ | ||
static fn ($m) => $m, | ||
$hk | ||
); | ||
} | ||
} |