Skip to content

Remove [const unit], and refactoring #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions lib/MonadPHP/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ public function __set($name, $value) {

public function __get($name) {
return $this->bind(function($obj) use($name) {
if (isset($obj->$name)) {
return $obj->$name;
}
return null;
return isset($obj->$name)
? $obj->$name
: null;
});
}

public function __call($name, array $args = array()) {
return $this->bind(function($obj) use ($name, $args) {
if (is_callable(array($obj, $name))) {
return call_user_func_array(array($obj, $name), $args);
}
return null;
return is_callable(array($obj, $name))
? call_user_func_array(array($obj, $name), $args)
: null;
});
}

Expand Down
2 changes: 0 additions & 2 deletions lib/MonadPHP/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

class Identity extends Monad {

const unit = "MonadPHP\Identity::unit";

}
21 changes: 7 additions & 14 deletions lib/MonadPHP/ListMonad.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace MonadPHP;

class ListMonad extends Monad {
use InvalidArgumentException;
use Traversable;

const unit = "MonadPHP\ListMonad::unit";
class ListMonad extends Monad {

public function __construct($value) {
if (!is_array($value) && !$value instanceof \Traversable) {
throw new \InvalidArgumentException('Must be traversable');
if (!is_array($value) && !$value instanceof Traversable) {
throw new InvalidArgumentException('Must be traversable');
}
return parent::__construct($value);
}
Expand All @@ -18,19 +19,11 @@ public function bind($function, array $args = array()) {
foreach ($this->value as $value) {
$result[] = $this->runCallback($function, $value, $args);
}
return $this::unit($result);
return static::unit($result);
}

public function extract() {
$ret = array();
foreach ($this->value as $value) {
if ($value instanceof Monad) {
$ret[] = $value->extract();
} else {
$ret[] = $value;
}
}
return $ret;
return array_map([get_called_class(), 'extractValue'], $this->value);
}

}
11 changes: 4 additions & 7 deletions lib/MonadPHP/Maybe.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

class Maybe extends Monad {

const unit = "MonadPHP\Maybe::unit";

public function bind($function){
if (!is_null($this->value)) {
return parent::bind($function);
}
return $this::unit(null);
public function bind($function) {
return is_null($this->value)
? static::unit(null)
: parent::bind($function);
}

}
20 changes: 11 additions & 9 deletions lib/MonadPHP/Monad.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ public function __construct($value) {
}

public static function unit($value) {
if ($value instanceof static) {
return $value;
}
return new static($value);
return $value instanceof static
? $value
: new static($value);
}

public function bind($function, array $args = array()) {
return $this::unit($this->runCallback($function, $this->value, $args));
return static::unit($this->runCallback($function, $this->value, $args));
}

public function extract() {
if ($this->value instanceof self) {
return $this->value->extract();
}
return $this->value;
return static::extractValue($this->value);
}

public static function extractValue($value) {
return $value instanceof self
? $value->extract()
: $value;
}

protected function runCallback($function, $value, array $args = array()) {
Expand Down
19 changes: 9 additions & 10 deletions lib/MonadPHP/Promise.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace MonadPHP;

class Promise extends Monad {
use BadMethodCallException;

const unit = "MonadPHP\Promise::unit";
class Promise extends Monad {

protected $isResolved = false;
protected $succeed = null;
Expand All @@ -19,22 +19,21 @@ public function __construct($success = null, $failure = null) {
}

public static function unit($success = null, $failure = null) {
return new Promise($success, $failure);
return new Promise($success, $failure);
}

public function bind($success, $failure) {
$obj = $this->unit($success, $failure);
if ($this->isResolved) {
$obj->resolve($this->succeed, $this->value);
} else {
$this->children[] = $obj;
}
$obj = static::unit($success, $failure);
$this->isResolved
? $obj->resolve($this->succeed, $this->value)
: $this->children[] = $obj;

return $obj;
}

protected function resolve($status, $value) {
if ($this->isResolved) {
throw new \BadMethodCallException('Promise already resolved');
throw new BadMethodCallException('Promise already resolved');
}
$this->value = $value;
$this->isResolved = true;
Expand Down
4 changes: 2 additions & 2 deletions test/MonadPHP/IdentityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class IdentityTest extends \PHPUnit_Framework_TestCase {

public function testBind() {
$monad = new Identity(1);
$this->assertEquals($monad->unit(1), $monad->bind('intval'));
$this->assertEquals($monad::unit(1), $monad->bind('intval'));
}

public function testBindUnit() {
$monad = new Identity(1);
$this->assertEquals($monad, $monad->bind($monad::unit));
$this->assertEquals($monad, $monad->bind([$monad, 'unit']));
}

public function testExtract() {
Expand Down