public static function unit($value) { if ($value instanceof static) { return $value; } return new static($value); }
// public static function unit($value) { return new static($value); }
// abstract public function fbind($function, array $args = array()); // abstract public function flatten();
abstract class Maybe extends Monad { abstract public function extractOrElse($val, array $args = array()); }
class Just extends Maybe { public function extractOrElse($val, array $args = array()) { return $this->value; } public function fbind($function, array $args = array()) { $res = $this->runCallback($function, $this->value, $args) if(res instanceof Maybe) return $res; else throw new \InvalidArgumentException('Returned value must be an instanceof Maybe monad'); } public function flatten() { if($this->value instanceof Maybe) return $this->value; else throw new Exception('Value of just is not an instance of Maybe monad'); } }
class Nothing extends Maybe { protected static $_instance = NULL; final private function __construct() { } final private function __clone() { } final public static function getInstance(){ if(null !== static::$_instance){ return static::$_instance; } static::$_instance = new static(); return static::$_instance; } public function extractOrElse($val, array $args = array()) { if (is_callable($val) || $val instanceof Closure) return call_user_func_array($val, $args); else return $val } public function bind($function, array $args = array()) { return $this; } public function fbind($function, array $args = array()) { return $this; } public function flatten() { throw new Exception("Nothing flatten call"); } }
function getGrandParentName($item) { $monad = new Maybe($item); $getParent = function($item) { // null, ! return $item->getParent(); }; $getName = function($item) { return $item->getName(); } return $monad ->bind($getParent) ->bind($getParent) ->bind($getName) ->extract(); }
function getGrandParentName($item) { $getParent = function($item) { return $item->getParent(); }; $getName = function($item) { return $item->getName(); } return $item ->fbind($getParent) ->fbind($getParent) ->bind($getName) ->extractOrElse("default"); }
Source: https://habr.com/ru/post/232071/
All Articles