Skip to content

Commit 34ed870

Browse files
authored
Merge pull request #104 from ilario-pierbattista/ilario-pierbattista-liftm2-not-working-with-io
liftM2 is not working with IO monads
2 parents b7ce6da + dffc7a5 commit 34ed870

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/Functional/functions.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -398,18 +398,10 @@ function liftM2(
398398
Monad $ma = null,
399399
Monad $mb = null
400400
) {
401-
return curryN(
402-
3,
403-
function (
404-
callable $transformation,
405-
Monad $ma,
406-
Monad $mb
407-
) {
408-
return bindM2(function ($a, $b) use ($transformation, $mb): Monad {
409-
return $mb::of($transformation($a, $b));
410-
}, $ma, $mb);
411-
}
412-
)(...func_get_args());
401+
return call_user_func_array(
402+
liftA2,
403+
func_get_args()
404+
);
413405
}
414406

415407
/**

test/Functional/LiftM2Test.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace test\Functional;
66

77
use FunctionalPHP\FantasyLand\Monad;
8+
use Widmogrod\Common\ValueOfInterface;
89
use Widmogrod\Functional as f;
910
use Widmogrod\Monad\Either;
11+
use Widmogrod\Monad\IO;
1012
use Widmogrod\Monad\Maybe;
1113

1214
class LiftM2Test extends \PHPUnit\Framework\TestCase
@@ -19,14 +21,13 @@ public function test_it_should_lift2M(
1921
Monad $mb,
2022
callable $transformation,
2123
string $expectedFQCN,
22-
$expectedExtracted = null
24+
callable $valueAssertion = null
2325
) {
2426
$mc = f\liftM2($transformation, $ma, $mb);
2527

2628
$this->assertInstanceOf($expectedFQCN, $mc);
27-
28-
if ($expectedExtracted !== null) {
29-
$this->assertSame($expectedExtracted, f\valueOf($mc));
29+
if ($valueAssertion !== null) {
30+
$valueAssertion($mc);
3031
}
3132
}
3233

@@ -36,6 +37,10 @@ public function monadsProvider()
3637
return $a + $b;
3738
};
3839

40+
$sameValueOf = f\curryN(2, function ($expected, ValueOfInterface $actual) {
41+
$this->assertSame($expected, f\valueOf($actual));
42+
});
43+
3944
return [
4045
'maybe all nothing' => [
4146
Maybe\nothing(),
@@ -60,32 +65,48 @@ public function monadsProvider()
6065
Maybe\just(2),
6166
$sumIntegers,
6267
Maybe\Just::class,
63-
3
68+
$sameValueOf(3)
6469
],
6570
'either all left' => [
6671
Either\left('a'),
6772
Either\left('b'),
6873
$sumIntegers,
69-
Either\Left::class
74+
Either\Left::class,
75+
$sameValueOf('a')
7076
],
7177
'either first right' => [
7278
Either\right(3),
7379
Either\left('b'),
7480
$sumIntegers,
75-
Either\Left::class
81+
Either\Left::class,
82+
$sameValueOf('b')
7683
],
7784
'either second right' => [
7885
Either\left('a'),
7986
Either\right(4),
8087
$sumIntegers,
81-
Either\Left::class
88+
Either\Left::class,
89+
$sameValueOf('a')
8290
],
8391
'either all right' => [
8492
Either\right(3),
8593
Either\right(4),
8694
$sumIntegers,
8795
Either\Right::class,
88-
7
96+
$sameValueOf(7)
97+
],
98+
'io' => [
99+
IO::of(function () {
100+
return 1;
101+
}),
102+
IO::of(function () {
103+
return 2;
104+
}),
105+
$sumIntegers,
106+
IO::class,
107+
function (IO $io) {
108+
$this->assertSame(3, $io->run());
109+
}
89110
]
90111
];
91112
}

0 commit comments

Comments
 (0)