Exemplo n.º 1
0
 /**
  * @param $f
  * @param $container
  * @return array|callable
  */
 protected static function __fmap($f, $container)
 {
     /**
      * If $container is a simple array, just defer to array_map
      */
     if (is_array($container)) {
         return Arrays::map($f, $container);
     }
     /**
      * If $container is a function, we defer to compose
      */
     if ($container instanceof Closure) {
         return Lambda::compose($f, $container);
     }
     /**
      * If $container implements the Traversable interface, we can foreach over it
      */
     if ($container instanceof Traversable) {
         $result = [];
         foreach ($container as $item) {
             $result[] = $f($item);
         }
         return $result;
     }
     /**
      * Otherwise we just need to defer to the instances' internal fmap definition
      */
     return $container->fmap($f);
 }
Exemplo n.º 2
0
 /**
  * Assign Properties
  *
  * Set/Update properties on the object using a key/value array
  *
  * @example
  * Object::assign(['value' => 'hi!'], new stdClass);
  * // object(stdClass)#1 (1) {
  * //   ["value"]=>
  * //   string(3) "hi!"
  * // }
  *
  * @type array props -> Object objOriginal -> Object objUpdated
  *
  * @param $props
  * @param $objOriginal
  * @return mixed
  */
 protected static function __assign($props, $objOriginal)
 {
     $obj = clone $objOriginal;
     /** @noinspection PhpParamsInspection */
     return Arrays::foldl(function ($obj, $setter) {
         return $setter($obj);
     }, $obj, Arrays::mapIndexed(Lambda::flip(self::setProp()), $props));
 }
Exemplo n.º 3
0
 protected static function __indexLens($index)
 {
     $arraySetter = Module::curry(function ($i, $arr, $val) {
         $arr[$i] = $val;
         return $arr;
     });
     /** @noinspection PhpParamsInspection */
     $indexLens = self::makeLens(Arrays::index(), $arraySetter);
     return $indexLens($index);
 }
Exemplo n.º 4
0
 private function firstTagOr($errorMessage, $name)
 {
     try {
         if (!isset($this->tags[$name])) {
             throw new \Exception('Tag not found.');
         }
         return Either::right(Arrays::head($this->tags[$name]));
     } catch (\Exception $e) {
         return Either::left($errorMessage);
     }
 }
Exemplo n.º 5
0
 protected static function __bind($f, $container)
 {
     if (is_array($container)) {
         $result = [];
         $concat = Arrays::using('concat');
         foreach ($container as $x) {
             $result = $concat($result, $f($x));
         }
         return $result;
     }
     return $container->bind($f);
 }
 private function dictToList($dict)
 {
     if (!is_array($dict)) {
         return $dict;
     }
     return Arrays::mapIndexed(function ($v, $k) {
         return [$k => $this->dictToList($v)];
     }, $dict);
 }
Exemplo n.º 7
0
 /**
  * Any
  *
  * Returns true given any values are truthy
  *
  * @example
  * Logic::any(true, false); // True
  *
  * @example
  * Logic::any(false, false); // False
  *
  * @type array -> Bool
  *
  * @param  array $arr Values to test
  * @return Bool are any values truthy
  */
 protected static function __any($arr)
 {
     /** @noinspection PhpParamsInspection */
     return Arrays::foldl(self::logicalOr(), false, $arr);
 }
Exemplo n.º 8
0
 private function getFunctions()
 {
     $filterModule = Arrays::filter(Lambda::compose(Logic::eq($this->module->name), Object::getProp('class')));
     return $filterModule($this->module->getMethods());
 }