Skip to content

Commit 295500a

Browse files
committed
wqAdd in helper callback contant ::unit to the monads
1 parent 5789968 commit 295500a

File tree

6 files changed

+14
-3
lines changed

6 files changed

+14
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,7 @@ Or, what if you want to deal with multi-dimensional arrays?
9595
var_dump($doubled->extract());
9696
// Prints array(array(2, 4), array(6, 8), array(10, 12))
9797

98+
There also exist helper constants on each of the monads to get a callback to the `unit` method:
9899

100+
$newMonad = $monad->bind(Maybe::unit);
101+
// Does the same thing as above

lib/MonadPHP/Identity.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44

55
class Identity extends Monad {
66

7-
}
7+
const unit = "MonadPHP\Identity::unit";
8+
9+
}

lib/MonadPHP/ListMonad.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class ListMonad extends Monad {
66

7+
const unit = "MonadPHP\ListMonad::unit";
8+
79
public function __construct($value) {
810
if (!is_array($value) && !$value instanceof \Traversible) {
911
throw new \InvalidArgumentException('Must be traversible');

lib/MonadPHP/Maybe.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class Maybe extends Monad {
66

7+
const unit = "MonadPHP\Maybe::unit";
8+
79
public function bind($function){
810
if (!is_null($this->value)) {
911
return parent::bind($function);

lib/MonadPHP/Promise.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class Promise extends Monad {
66

7+
const unit = "MonadPHP\Promise::unit";
8+
79
protected $isResolved = false;
810
protected $succeed = null;
911

test/MonadPHP/IdentityTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public function testBind() {
1111

1212
public function testBindUnit() {
1313
$monad = new Identity(1);
14-
$this->assertEquals($monad, $monad->bind(function($value) use ($monad) { return $monad->unit($value); }));
14+
$this->assertEquals($monad, $monad->bind($monad::unit));
1515
}
1616

1717
public function testExtract() {
1818
$monad = new Identity(new Maybe(1));
1919
$this->assertEquals(1, $monad->extract());
2020
}
2121

22-
}
22+
}

0 commit comments

Comments
 (0)