Skip to content

Commit

Permalink
Merge pull request #104 from ilario-pierbattista/ilario-pierbattista-…
Browse files Browse the repository at this point in the history
…liftm2-not-working-with-io

liftM2 is not working with IO monads
  • Loading branch information
widmogrod authored Jun 20, 2020
2 parents b7ce6da + dffc7a5 commit 34ed870
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
16 changes: 4 additions & 12 deletions src/Functional/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,18 +398,10 @@ function liftM2(
Monad $ma = null,
Monad $mb = null
) {
return curryN(
3,
function (
callable $transformation,
Monad $ma,
Monad $mb
) {
return bindM2(function ($a, $b) use ($transformation, $mb): Monad {
return $mb::of($transformation($a, $b));
}, $ma, $mb);
}
)(...func_get_args());
return call_user_func_array(
liftA2,
func_get_args()
);
}

/**
Expand Down
39 changes: 30 additions & 9 deletions test/Functional/LiftM2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace test\Functional;

use FunctionalPHP\FantasyLand\Monad;
use Widmogrod\Common\ValueOfInterface;
use Widmogrod\Functional as f;
use Widmogrod\Monad\Either;
use Widmogrod\Monad\IO;
use Widmogrod\Monad\Maybe;

class LiftM2Test extends \PHPUnit\Framework\TestCase
Expand All @@ -19,14 +21,13 @@ public function test_it_should_lift2M(
Monad $mb,
callable $transformation,
string $expectedFQCN,
$expectedExtracted = null
callable $valueAssertion = null
) {
$mc = f\liftM2($transformation, $ma, $mb);

$this->assertInstanceOf($expectedFQCN, $mc);

if ($expectedExtracted !== null) {
$this->assertSame($expectedExtracted, f\valueOf($mc));
if ($valueAssertion !== null) {
$valueAssertion($mc);
}
}

Expand All @@ -36,6 +37,10 @@ public function monadsProvider()
return $a + $b;
};

$sameValueOf = f\curryN(2, function ($expected, ValueOfInterface $actual) {
$this->assertSame($expected, f\valueOf($actual));
});

return [
'maybe all nothing' => [
Maybe\nothing(),
Expand All @@ -60,32 +65,48 @@ public function monadsProvider()
Maybe\just(2),
$sumIntegers,
Maybe\Just::class,
3
$sameValueOf(3)
],
'either all left' => [
Either\left('a'),
Either\left('b'),
$sumIntegers,
Either\Left::class
Either\Left::class,
$sameValueOf('a')
],
'either first right' => [
Either\right(3),
Either\left('b'),
$sumIntegers,
Either\Left::class
Either\Left::class,
$sameValueOf('b')
],
'either second right' => [
Either\left('a'),
Either\right(4),
$sumIntegers,
Either\Left::class
Either\Left::class,
$sameValueOf('a')
],
'either all right' => [
Either\right(3),
Either\right(4),
$sumIntegers,
Either\Right::class,
7
$sameValueOf(7)
],
'io' => [
IO::of(function () {
return 1;
}),
IO::of(function () {
return 2;
}),
$sumIntegers,
IO::class,
function (IO $io) {
$this->assertSame(3, $io->run());
}
]
];
}
Expand Down

0 comments on commit 34ed870

Please sign in to comment.