From e13ef91e3e08ee0f72bbde46eaa10f2b5cab1461 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 19 Oct 2021 12:33:24 +0200 Subject: [PATCH] Update `identity` instances. --- .../Identity/IdentityApplicative.php | 34 +++++++----- src/Instances/Identity/IdentityApply.php | 38 ++++++-------- src/Instances/Identity/IdentityFoldable.php | 14 ++--- src/Instances/Identity/IdentityFunctor.php | 24 ++++----- src/Instances/Identity/IdentityMonad.php | 52 ++++++++++--------- .../Identity/IdentityTraversable.php | 34 ++++++------ 6 files changed, 94 insertions(+), 102 deletions(-) diff --git a/src/Instances/Identity/IdentityApplicative.php b/src/Instances/Identity/IdentityApplicative.php index a75995d..aa328cf 100644 --- a/src/Instances/Identity/IdentityApplicative.php +++ b/src/Instances/Identity/IdentityApplicative.php @@ -10,22 +10,24 @@ use Marcosh\LamPHPda\Typeclass\Applicative; /** - * @implements Applicative - * * @psalm-immutable + * + * @implements Applicative */ final class IdentityApplicative implements Applicative { /** + * @psalm-suppress LessSpecificImplementedReturnType + * + * @psalm-pure + * * @template A * @template B + * * @param callable(A): B $f * @param HK1 $a - * @return Identity - * - * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @return Identity */ public function map(callable $f, $a): Identity { @@ -33,15 +35,17 @@ public function map(callable $f, $a): Identity } /** + * @psalm-suppress LessSpecificImplementedReturnType + * + * @psalm-pure + * * @template A * @template B + * * @param HK1 $f * @param HK1 $a - * @return Identity - * - * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @return Identity */ public function apply(HK1 $f, HK1 $a): Identity { @@ -49,13 +53,15 @@ public function apply(HK1 $f, HK1 $a): Identity } /** - * @template A - * @param A $a - * @return Identity + * @psalm-suppress LessSpecificImplementedReturnType * * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @template A + * + * @param A $a + * + * @return Identity */ public function pure($a): Identity { diff --git a/src/Instances/Identity/IdentityApply.php b/src/Instances/Identity/IdentityApply.php index 9c8f180..8223999 100644 --- a/src/Instances/Identity/IdentityApply.php +++ b/src/Instances/Identity/IdentityApply.php @@ -10,22 +10,24 @@ use Marcosh\LamPHPda\Typeclass\Apply; /** - * @implements Apply - * * @psalm-immutable + * + * @implements Apply */ final class IdentityApply implements Apply { /** + * @psalm-suppress LessSpecificImplementedReturnType + * + * @psalm-pure + * * @template A * @template B + * * @param callable(A): B $f * @param HK1 $a - * @return Identity - * - * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @return Identity */ public function map(callable $f, $a): Identity { @@ -33,33 +35,23 @@ public function map(callable $f, $a): Identity } /** + * @psalm-suppress LessSpecificImplementedReturnType + * + * @psalm-pure + * * @template A * @template B + * * @param HK1 $f * @param HK1 $a - * @return Identity - * - * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @return Identity */ public function apply(HK1 $f, HK1 $a): Identity { $identityF = Identity::fromBrand($f); $identityA = Identity::fromBrand($a); - return $identityA->eval( - /** - * @param A $value - * @return Identity - */ - fn($value) => $identityF->eval( - /** - * @psalm-param callable(A): B $g - * @psalm-return Identity - */ - fn($g) => Identity::wrap($g($value)) - ) - ); + return Identity::wrap(($identityF->unwrap())($identityA->unwrap())); } } diff --git a/src/Instances/Identity/IdentityFoldable.php b/src/Instances/Identity/IdentityFoldable.php index a7e9df6..2892f6d 100644 --- a/src/Instances/Identity/IdentityFoldable.php +++ b/src/Instances/Identity/IdentityFoldable.php @@ -10,30 +10,26 @@ use Marcosh\LamPHPda\Typeclass\Foldable; /** - * @implements Foldable - * * @psalm-immutable + * + * @implements Foldable */ final class IdentityFoldable implements Foldable { /** * @template A * @template B + * * @param callable(A, B): B $f * @param B $b * @param HK1 $a + * * @return B */ public function foldr(callable $f, $b, HK1 $a) { $identityA = Identity::fromBrand($a); - return $identityA->eval( - /** - * @param A $a - * @return B - */ - fn ($a) => $f($a, $b) - ); + return $f($identityA->unwrap(), $b); } } diff --git a/src/Instances/Identity/IdentityFunctor.php b/src/Instances/Identity/IdentityFunctor.php index 5789e5d..5ba762f 100644 --- a/src/Instances/Identity/IdentityFunctor.php +++ b/src/Instances/Identity/IdentityFunctor.php @@ -10,31 +10,29 @@ use Marcosh\LamPHPda\Typeclass\Functor; /** - * @implements Functor - * * @psalm-immutable + * + * @implements Functor */ final class IdentityFunctor implements Functor { /** + * @psalm-suppress LessSpecificImplementedReturnType + * + * @psalm-pure + * * @template A * @template B + * * @param callable(A): B $f * @param HK1 $a - * @return Identity - * - * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @return Identity */ public function map(callable $f, $a): Identity { - return Identity::fromBrand($a)->eval( - /** - * @param A $value - * @return Identity - */ - fn($value) => Identity::wrap($f($value)) - ); + $identityA = Identity::fromBrand($a); + + return Identity::wrap($f($identityA->unwrap())); } } diff --git a/src/Instances/Identity/IdentityMonad.php b/src/Instances/Identity/IdentityMonad.php index 62852ca..4719cd6 100644 --- a/src/Instances/Identity/IdentityMonad.php +++ b/src/Instances/Identity/IdentityMonad.php @@ -10,22 +10,24 @@ use Marcosh\LamPHPda\Typeclass\Monad; /** - * @implements Monad - * * @psalm-immutable + * + * @implements Monad */ final class IdentityMonad implements Monad { /** + * @psalm-suppress LessSpecificImplementedReturnType + * + * @psalm-pure + * * @template A * @template B + * * @param callable(A): B $f * @param HK1 $a - * @return Identity - * - * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @return Identity */ public function map(callable $f, $a): Identity { @@ -33,15 +35,17 @@ public function map(callable $f, $a): Identity } /** + * @psalm-suppress LessSpecificImplementedReturnType + * + * @psalm-pure + * * @template A * @template B + * * @param HK1 $f * @param HK1 $a - * @return Identity - * - * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @return Identity */ public function apply(HK1 $f, HK1 $a): Identity { @@ -49,13 +53,15 @@ public function apply(HK1 $f, HK1 $a): Identity } /** - * @template A - * @param A $a - * @return Identity + * @psalm-suppress LessSpecificImplementedReturnType * * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @template A + * + * @param A $a + * + * @return Identity */ public function pure($a): Identity { @@ -63,26 +69,22 @@ public function pure($a): Identity } /** + * @psalm-suppress LessSpecificImplementedReturnType + * + * @psalm-pure + * * @template A * @template B + * * @param HK1 $a * @param callable(A): HK1 $f - * @return Identity - * - * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @return Identity */ public function bind(HK1 $a, callable $f): Identity { $identityA = Identity::fromBrand($a); - return $identityA->eval( - /** - * @param A $a - * @return Identity - */ - fn($a) => Identity::fromBrand($f($a)) - ); + return Identity::fromBrand($f($identityA->unwrap())); } } diff --git a/src/Instances/Identity/IdentityTraversable.php b/src/Instances/Identity/IdentityTraversable.php index 646cd3e..665fa9c 100644 --- a/src/Instances/Identity/IdentityTraversable.php +++ b/src/Instances/Identity/IdentityTraversable.php @@ -12,22 +12,24 @@ use Marcosh\LamPHPda\Typeclass\Traversable; /** - * @implements Traversable - * * @psalm-immutable + * + * @implements Traversable */ final class IdentityTraversable implements Traversable { /** + * @psalm-pure + * + * @psalm-suppress LessSpecificImplementedReturnType + * * @template A * @template B + * * @param callable(A): B $f * @param HK1 $a - * @return Identity - * - * @psalm-pure * - * @psalm-suppress LessSpecificImplementedReturnType + * @return Identity */ public function map(callable $f, $a): Identity { @@ -37,9 +39,11 @@ public function map(callable $f, $a): Identity /** * @template A * @template B + * * @param callable(A, B): B $f * @param B $b * @param HK1 $a + * * @return B */ public function foldr(callable $f, $b, HK1 $a) @@ -48,29 +52,23 @@ public function foldr(callable $f, $b, HK1 $a) } /** + * @psalm-suppress ImplementedReturnTypeMismatch + * @psalm-suppress LessSpecificImplementedReturnType + * * @template F of Brand * @template A * @template B + * * @param Applicative $applicative * @param callable(A): HK1 $f * @param HK1 $a - * @return HK1> * - * @psalm-suppress ImplementedReturnTypeMismatch - * @psalm-suppress LessSpecificImplementedReturnType + * @return HK1> */ public function traverse(Applicative $applicative, callable $f, HK1 $a): HK1 { $identityA = Identity::fromBrand($a); - return $identityA->eval( - /** - * @param A $a - * @return HK1> - * - * @psalm-suppress InvalidArgument - */ - fn($a) => $applicative->map([Identity::class, 'wrap'], $f($a)) - ); + return $applicative->map([Identity::class, 'wrap'], $f($identityA->unwrap())); } }