Skip to content

Commit

Permalink
Update identity instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol authored and marcosh committed Oct 21, 2021
1 parent 4ab02a2 commit e13ef91
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 102 deletions.
34 changes: 20 additions & 14 deletions src/Instances/Identity/IdentityApplicative.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,58 @@
use Marcosh\LamPHPda\Typeclass\Applicative;

/**
* @implements Applicative<IdentityBrand>
*
* @psalm-immutable
*
* @implements Applicative<IdentityBrand>
*/
final class IdentityApplicative implements Applicative
{
/**
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @template A
* @template B
*
* @param callable(A): B $f
* @param HK1<IdentityBrand, A> $a
* @return Identity<B>
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @return Identity<B>
*/
public function map(callable $f, $a): Identity
{
return (new IdentityFunctor())->map($f, $a);
}

/**
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @template A
* @template B
*
* @param HK1<IdentityBrand, callable(A): B> $f
* @param HK1<IdentityBrand, A> $a
* @return Identity<B>
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @return Identity<B>
*/
public function apply(HK1 $f, HK1 $a): Identity
{
return (new IdentityApply())->apply($f, $a);
}

/**
* @template A
* @param A $a
* @return Identity<A>
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @template A
*
* @param A $a
*
* @return Identity<A>
*/
public function pure($a): Identity
{
Expand Down
38 changes: 15 additions & 23 deletions src/Instances/Identity/IdentityApply.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,48 @@
use Marcosh\LamPHPda\Typeclass\Apply;

/**
* @implements Apply<IdentityBrand>
*
* @psalm-immutable
*
* @implements Apply<IdentityBrand>
*/
final class IdentityApply implements Apply
{
/**
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @template A
* @template B
*
* @param callable(A): B $f
* @param HK1<IdentityBrand, A> $a
* @return Identity<B>
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @return Identity<B>
*/
public function map(callable $f, $a): Identity
{
return (new IdentityFunctor())->map($f, $a);
}

/**
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @template A
* @template B
*
* @param HK1<IdentityBrand, callable(A): B> $f
* @param HK1<IdentityBrand, A> $a
* @return Identity<B>
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @return Identity<B>
*/
public function apply(HK1 $f, HK1 $a): Identity
{
$identityF = Identity::fromBrand($f);
$identityA = Identity::fromBrand($a);

return $identityA->eval(
/**
* @param A $value
* @return Identity<B>
*/
fn($value) => $identityF->eval(
/**
* @psalm-param callable(A): B $g
* @psalm-return Identity<B>
*/
fn($g) => Identity::wrap($g($value))
)
);
return Identity::wrap(($identityF->unwrap())($identityA->unwrap()));
}
}
14 changes: 5 additions & 9 deletions src/Instances/Identity/IdentityFoldable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,26 @@
use Marcosh\LamPHPda\Typeclass\Foldable;

/**
* @implements Foldable<IdentityBrand>
*
* @psalm-immutable
*
* @implements Foldable<IdentityBrand>
*/
final class IdentityFoldable implements Foldable
{
/**
* @template A
* @template B
*
* @param callable(A, B): B $f
* @param B $b
* @param HK1<IdentityBrand, A> $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);
}
}
24 changes: 11 additions & 13 deletions src/Instances/Identity/IdentityFunctor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,29 @@
use Marcosh\LamPHPda\Typeclass\Functor;

/**
* @implements Functor<IdentityBrand>
*
* @psalm-immutable
*
* @implements Functor<IdentityBrand>
*/
final class IdentityFunctor implements Functor
{
/**
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @template A
* @template B
*
* @param callable(A): B $f
* @param HK1<IdentityBrand, A> $a
* @return Identity<B>
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @return Identity<B>
*/
public function map(callable $f, $a): Identity
{
return Identity::fromBrand($a)->eval(
/**
* @param A $value
* @return Identity<B>
*/
fn($value) => Identity::wrap($f($value))
);
$identityA = Identity::fromBrand($a);

return Identity::wrap($f($identityA->unwrap()));
}
}
52 changes: 27 additions & 25 deletions src/Instances/Identity/IdentityMonad.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,81 @@
use Marcosh\LamPHPda\Typeclass\Monad;

/**
* @implements Monad<IdentityBrand>
*
* @psalm-immutable
*
* @implements Monad<IdentityBrand>
*/
final class IdentityMonad implements Monad
{
/**
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @template A
* @template B
*
* @param callable(A): B $f
* @param HK1<IdentityBrand, A> $a
* @return Identity<B>
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @return Identity<B>
*/
public function map(callable $f, $a): Identity
{
return (new IdentityFunctor())->map($f, $a);
}

/**
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @template A
* @template B
*
* @param HK1<IdentityBrand, callable(A): B> $f
* @param HK1<IdentityBrand, A> $a
* @return Identity<B>
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @return Identity<B>
*/
public function apply(HK1 $f, HK1 $a): Identity
{
return (new IdentityApply())->apply($f, $a);
}

/**
* @template A
* @param A $a
* @return Identity<A>
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @template A
*
* @param A $a
*
* @return Identity<A>
*/
public function pure($a): Identity
{
return (new IdentityApplicative())->pure($a);
}

/**
* @psalm-suppress LessSpecificImplementedReturnType
*
* @psalm-pure
*
* @template A
* @template B
*
* @param HK1<IdentityBrand, A> $a
* @param callable(A): HK1<IdentityBrand, B> $f
* @return Identity<B>
*
* @psalm-pure
*
* @psalm-suppress LessSpecificImplementedReturnType
* @return Identity<B>
*/
public function bind(HK1 $a, callable $f): Identity
{
$identityA = Identity::fromBrand($a);

return $identityA->eval(
/**
* @param A $a
* @return Identity<B>
*/
fn($a) => Identity::fromBrand($f($a))
);
return Identity::fromBrand($f($identityA->unwrap()));
}
}
Loading

0 comments on commit e13ef91

Please sign in to comment.