Exemple #1
0
 public static function collect()
 {
     $collector = V::fn_w(function ($array, Value $v) use(&$collector) {
         if (!($v->isError() || $v->isApplicable()) && $v->get() instanceof Stop) {
             // Postprocessing of the collected values.
             // We need to check weather there are errors in the collection
             // to be able to get errors appropriately.
             $errors = array();
             $vals = array_map(function ($v) use(&$errors) {
                 $v = $v->force();
                 if ($v->isError()) {
                     $errors[] = $v;
                     return $v;
                 }
                 if ($v->isApplicable()) {
                     return $v;
                 }
                 return $v->get();
             }, $array->get());
             if (count($errors) > 0) {
                 return V::error("Collection contains errors.", "_collect", $errors);
             }
             return V::val($vals, "collect");
         }
         $array = $array->get();
         $array[] = $v->force();
         return $collector->apply(V::val($array));
     });
     return $collector->apply(V::val(array()));
 }
Exemple #2
0
 public final function satisfies(FunctionValue $predicate, $error)
 {
     C::guardIsString($error);
     C::guardHasArity($predicate, 1);
     return $this->map(V::fn_w(function ($value) use($predicate, $error) {
         if (!$predicate->apply($value)->get()) {
             return V::error($error, $value->origin());
         }
         return $value;
     }));
 }
 /**
  * One can get a dictionary out of an error that contains the error messages
  * from the error itself and all the other errors that led to it.
  */
 public function testErrorToDict()
 {
     $a = V::error("a", "a");
     $b = V::error("b", "b");
     $c = V::error("c", "c");
     $x1 = V::error("1", "x");
     $x2 = V::error("2", "x");
     $all = V::error("all", "all", array($a, $b, $c, $x1, $x2));
     $dict = $all->toDict();
     $this->assertArrayHasKey("a", $dict);
     $this->assertArrayHasKey("b", $dict);
     $this->assertArrayHasKey("c", $dict);
     $this->assertArrayHasKey("x", $dict);
     $this->assertArrayHasKey("all", $dict);
     $this->assertCount(5, $dict);
     $this->assertEquals($dict["a"], array("a"));
     $this->assertEquals($dict["b"], array("b"));
     $this->assertEquals($dict["c"], array("c"));
     $this->assertEquals($dict["x"], array("1", "2"));
     $this->assertEquals($dict["all"], array("all"));
 }
 private function rawActualCall()
 {
     if ($this->_unwrap_args) {
         $args = array();
         $errors = array();
         $this->evalArgs($args, $errors);
         if (count($errors) > 0) {
             return V::error("Function arguments contain errors.", $this->origin(), $errors);
         }
     } else {
         $args = $this->_args;
     }
     return call_user_func_array($this->_function, $args);
 }