Skip to content

Commit f8f37d9

Browse files
committed
Maybe is not foldable
1 parent f61208f commit f8f37d9

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

src/Monad/Maybe/Just.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,15 @@ public function extract()
8181
{
8282
return $this->value;
8383
}
84+
85+
/**
86+
* foldl _ z Nothing = z
87+
* foldl f z (Just x) = f z x
88+
*
89+
* @inheritdoc
90+
*/
91+
public function reduce(callable $function, $accumulator)
92+
{
93+
return $function($accumulator, $this->value);
94+
}
8495
}

src/Monad/Maybe/Maybe.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
interface Maybe extends
1111
FantasyLand\Monad,
12+
FantasyLand\Foldable,
1213
Common\ValueOfInterface,
1314
FantasyLand\Monoid
1415
{

src/Monad/Maybe/Nothing.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,15 @@ public function extract()
7373
{
7474
return null;
7575
}
76+
77+
/**
78+
* foldl _ z Nothing = z
79+
* foldl f z (Just x) = f z x
80+
*
81+
* @inheritdoc
82+
*/
83+
public function reduce(callable $function, $accumulator)
84+
{
85+
return $accumulator;
86+
}
7687
}

test/Functional/ConcatTest.php

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

77
use Widmogrod\Functional as f;
8+
use Widmogrod\Monad\Maybe\Just;
9+
use Widmogrod\Monad\Maybe\Nothing;
810

911
class ConcatTest extends \PHPUnit\Framework\TestCase
1012
{
@@ -59,6 +61,14 @@ public function provideData()
5961
3
6062
]),
6163
],
64+
'Just of lists' => [
65+
'$array' => Just::of(f\fromIterable(['a', 1, 3])),
66+
'$expected' => f\fromIterable(['a', 1, 3]),
67+
],
68+
'Nothing of lists' => [
69+
'$array' => Nothing::mempty(),
70+
'$expected' => f\fromNil()
71+
],
6272
];
6373
}
6474
}

0 commit comments

Comments
 (0)