Пример #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()));
 }
Пример #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;
     }));
 }
Пример #3
0
 public function composeWith(FunctionValue $other)
 {
     return V::fn_w(function ($value) use($other) {
         $res = $other->apply($value)->force();
         return $this->apply($res)->force();
     });
 }