Exemplo n.º 1
0
 /**
  * This method causes the iterator to advance to the next object.
  *
  * @access public
  * @final
  * @return IBool\Type                                       whether there are more objects
  */
 public final function next() : IBool\Type
 {
     $this->i = IInt32\Module::increment($this->i);
     return IBool\Type::box($this->valid());
 }
Exemplo n.º 2
0
 /**
  * This constructor creates a new runtime exception.
  *
  * @access public
  * @param string $message                                   the error message
  * @param array $values                                     the value to be formatted
  * @param IInt32\Type $code                                  the exception code
  */
 public function __construct($message = '', array $values = null, IInt32\Type $code = null)
 {
     parent::__construct(empty($values) ? (string) $message : strtr((string) $message, $values), IInt32\Module::nvl($code)->unbox());
 }
Exemplo n.º 3
0
 /**
  * This method provides the data for testing the "foldRight" method.
  *
  * @return array
  */
 public function data_foldRight()
 {
     $predicate = function (IInt32\Type $c, IInt32\Type $x) : IInt32\Type {
         return IInt32\Module::add($c, $x);
     };
     $data = array(array(array(array(), $predicate), array(0)), array(array(array(1), $predicate), array(1)), array(array(array(1, 2), $predicate), array(3)), array(array(array(1, 2, 3), $predicate), array(6)), array(array(array(1, 2, 3, 4), $predicate), array(10)));
     return $data;
 }
Exemplo n.º 4
0
 /**
  * This method returns the numerically lowest value.
  *
  * @access public
  * @static
  * @param IInt32\Type $x                                    the left operand
  * @param IInt32\Type $y                                    the right operand
  * @return IInt32\Type                                      the minimum value
  */
 public static function min(IInt32\Type $x, IInt32\Type $y) : IInt32\Type
 {
     return IInt32\Module::compare($x, $y)->unbox() <= 0 ? $x : $y;
 }
Exemplo n.º 5
0
 /**
  * This method returns the collection as a linked list.
  *
  * @access public
  * @static
  * @param IString\Type $xs                                  the operand
  * @return ILinkedList\Type                                 the collection as a linked list
  */
 public static function toLinkedList(IString\Type $xs) : ILinkedList\Type
 {
     $length = $xs->length();
     $zs = ILinkedList\Type::nil();
     for ($i = IInt32\Module::decrement($length); IInt32\Module::ge($i, IInt32\Type::zero())->unbox(); $i = IInt32\Module::decrement($i)) {
         $zs = ILinkedList\Type::cons($xs->item($i), $zs);
     }
     return $zs;
 }
Exemplo n.º 6
0
 /**
  * This method tests the "isOdd" method.
  *
  * @dataProvider data_isOdd
  */
 public function test_isOdd(array $provided, array $expected)
 {
     $p0 = IInt32\Module::isOdd(IInt32\Type::box($provided[0]));
     $e0 = $expected[0];
     $this->assertInstanceOf('\\Saber\\Data\\IBool\\Type', $p0);
     $this->assertSame($e0, $p0->unbox());
 }
Exemplo n.º 7
0
 /**
  * This method returns the length of this linked list.
  *
  * @access public
  * @final
  * @return IInt32\Type                                      the length of this linked list
  */
 public final function length() : IInt32\Type
 {
     $c = IInt32\Type::zero();
     for ($zs = $this; !$zs->__isEmpty(); $zs = $zs->tail()) {
         $c = IInt32\Module::increment($c);
     }
     return $c;
 }
Exemplo n.º 8
0
 /**
  * This method returns a random number with the range of x and y.
  *
  * @access public
  * @static
  * @param IInteger\Type $x                                  the min operand
  * @param IInt32\Type $y                                    the max operand
  * @return IInteger\Type                                    the result
  */
 public static function random(IInteger\Type $x = null, IInt32\Type $y = null) : IInteger\Type
 {
     return IInteger\Type::box(gmp_strval(gmp_random(IInt32\Module::nvl($y, IInt32\Type::one())->unbox())));
 }
Exemplo n.º 9
0
 /**
  * This method evaluates whether the left operand is identical to the right operand.
  *
  * @access public
  * @static
  * @param IHashMap\Type $xs                                 the left operand
  * @param Core\Type $ys                                     the right operand
  * @return IBool\Type                                       whether the left operand is identical
  *                                                          to the right operand
  */
 public static function id(IHashMap\Type $xs, Core\Type $ys) : IBool\Type
 {
     // ===
     if ($ys !== null && $xs->__typeOf() === $ys->__typeOf()) {
         if (IInt32\Module::eq($xs->size(), $ys->size())) {
             return IBool\Type::box((string) serialize($xs) == (string) serialize($ys));
         }
     }
     return IBool\Type::false();
 }
Exemplo n.º 10
0
 /**
  * This method returns a value as a boxed object.  A value is typically a PHP typed
  * primitive or object.  It is considered type-safe.
  *
  * @access public
  * @static
  * @param mixed $value                                      the value(s) to be boxed
  * @return IRatio\Type                                      the boxed object
  */
 public static function make($value) : IRatio\Type
 {
     $values = is_array($value) ? $value : func_get_args();
     $values = array_map(function ($value) {
         return $value instanceof IInt32\Type ? $value : IInt32\Type::make($value);
     }, $values);
     $denominator = $values[1];
     $signum = IInt32\Module::signum($denominator)->unbox();
     if ($signum == 0) {
         throw new Throwable\InvalidArgument\Exception('Unable to create ratio. Denominator must not be zero.');
     }
     $numerator = $values[0];
     if (IInt32\Module::signum($numerator)->unbox() == 0) {
         return IRatio\Type::zero();
     }
     if ($signum < 0) {
         $numerator = IInt32\Module::negate($numerator);
         $denominator = IInt32\Module::negate($denominator);
     }
     $gcd = IInt32\Module::gcd($numerator, $denominator);
     if (!IInt32\Module::eq($gcd, IInt32\Type::one())->unbox()) {
         $numerator = IInt32\Module::divide($numerator, $gcd);
         $denominator = IInt32\Module::divide($denominator, $gcd);
     }
     return new IRatio\Type($numerator, $denominator);
 }
Exemplo n.º 11
0
 /**
  * This method compares the operands for order.
  *
  * @access public
  * @static
  * @param ITuple\Type $xs                                   the left operand
  * @param ITuple\Type $ys                                   the right operand
  * @return ITrit\Type                                       the order as to whether the left
  *                                                          operand is less than, equals to,
  *                                                          or greater than the right operand
  */
 public static function compare(ITuple\Type $xs, ITuple\Type $ys) : ITrit\Type
 {
     $xsl = $xs->length();
     $ysl = $ys->length();
     $length = IInt32\Module::min($xsl, $ysl);
     for ($i = IInt32\Type::zero(); IInt32\Module::lt($i, $length)->unbox(); $i = IInt32\Module::increment($i)) {
         $r = $xs->item($i)->compare($ys->item($i));
         if ($r->unbox() != 0) {
             return $r;
         }
     }
     return ITrit\Type::box($xsl->unbox() <=> $ysl->unbox());
 }
Exemplo n.º 12
0
 /**
  * This method returns the result of rounding this object's value.
  *
  * @access public
  * @static
  * @param IFloat\Type $x                                    the operand
  * @param IInt32\Type $precision                            the precision to use when rounding
  * @return IFloat\Type                                      the result
  */
 public static function round(IFloat\Type $x, IInt32\Type $precision = null) : IFloat\Type
 {
     return IFloat\Type::box(round($x->unbox(), IInt32\Module::nvl($precision)->unbox()));
 }
Exemplo n.º 13
0
 /**
  * This method returns whether the operand is a negative number.
  *
  * @access public
  * @static
  * @param IRatio\Type $x                                    the object to be evaluated
  * @return IBool\Type                                       whether the operand is a negative
  *                                                          number
  */
 public static function isNegative(IRatio\Type $x) : IBool\Type
 {
     $a = IInt32\Module::isNegative($x->numerator())->unbox();
     $b = IInt32\Module::isNegative($x->denominator())->unbox();
     return IBool\Type::box(($a || $b) && $a != $b);
 }
Exemplo n.º 14
0
 /**
  * This method returns each item in this collection until the predicate fails.
  *
  * @access public
  * @static
  * @param ILinkedList\Type $xs                              the left operand
  * @param callable $predicate                               the predicate function to be used
  * @return ILinkedList\Type                                 the list
  */
 public static function takeWhile(ILinkedList\Type $xs, callable $predicate) : ILinkedList\Type
 {
     $start = ILinkedList\Type::nil();
     $tail = null;
     $taking = true;
     $i = IInt32\Type::zero();
     for ($zs = $xs; !$zs->__isEmpty() && $taking; $zs = $zs->tail()) {
         $z = $zs->head();
         if ($predicate($z, $i)->unbox()) {
             $ys = ILinkedList\Type::cons($z);
             if ($tail !== null) {
                 $tail->tail = $ys;
             } else {
                 $start = $ys;
             }
             $tail = $ys;
         } else {
             $taking = false;
         }
         $i = IInt32\Module::increment($i);
     }
     return $start;
 }