Ejemplo n.º 1
0
 /**
  * This method tests the "iterator" method.
  */
 public function test_iterator()
 {
     $p0 = IOption\Type::some(IInt32\Type::zero());
     $e0 = array(0);
     $this->assertInstanceOf('\\Saber\\Data\\IOption\\Some\\Type', $p0);
     $this->assertSame(count($e0), $p0->__size());
     $p1 = IOption\Module::iterator($p0);
     $e1 = 0;
     $this->assertInstanceOf('\\Saber\\Data\\IOption\\Iterator', $p1);
     foreach ($p1 as $i => $item) {
         $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $i);
         $this->assertSame($e1, $i->unbox());
         $this->assertInstanceOf('\\Saber\\Core\\Type', $item);
         $this->assertSame($e0[$e1], $item->unbox());
         $e1++;
     }
 }
Ejemplo n.º 2
0
 /**
  * This method returns a pair of hash maps: those items that satisfy the predicate and
  * those items that do not satisfy the predicate.
  *
  * @access public
  * @static
  * @param IHashMap\Type $xs                                 the hash map to be partitioned
  * @param callable $predicate                               the predicate function to be used
  * @return ITuple\Type                                      the results
  */
 public static function partition(IHashMap\Type $xs, callable $predicate) : ITuple\Type
 {
     $passed = IHashMap\Type::empty_();
     $failed = IHashMap\Type::empty_();
     $xi = IHashMap\Module::iterator($xs);
     $i = IInt32\Type::zero();
     foreach ($xi as $k => $v) {
         $entry = ITuple\Type::box2($k, $v);
         if ($predicate($entry, $i)->unbox()) {
             $passed->putEntry($entry->first(), $entry->second());
         } else {
             $failed->putEntry($entry->first(), $entry->second());
         }
     }
     return ITuple\Type::box2($passed, $failed);
 }
Ejemplo n.º 3
0
 /**
  * This method tests the "singletons" methods.
  */
 public function test_singletons()
 {
     $p0 = IInt32\Type::negative();
     $e0 = IInt32\Type::negative();
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $p0);
     $this->assertSame($e0->__hashCode(), $p0->__hashCode());
     $p1 = $p0->unbox();
     $e1 = -1;
     $this->assertInternalType('integer', $p1);
     $this->assertSame($e1, $p1);
     $p2 = IInt32\Type::zero();
     $e2 = IInt32\Type::zero();
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $p2);
     $this->assertSame($e2->__hashCode(), $p2->__hashCode());
     $p3 = $p2->unbox();
     $e3 = 0;
     $this->assertInternalType('integer', $p3);
     $this->assertSame($e3, $p3);
     $p4 = IInt32\Type::one();
     $e4 = IInt32\Type::one();
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $p4);
     $this->assertSame($e4->__hashCode(), $p4->__hashCode());
     $p5 = $p4->unbox();
     $e5 = 1;
     $this->assertInternalType('integer', $p5);
     $this->assertSame($e5, $p5);
 }
Ejemplo n.º 4
0
 /**
  * This method rewinds the iterator.
  *
  * @access public
  * @final
  */
 public final function rewind()
 {
     $this->i = IInt32\Type::zero();
 }
Ejemplo n.º 5
0
 /**
  * This method returns the first object in the collection that passes the truthy test, if any.
  *
  * @access public
  * @static
  * @param IOption\Type $xs                                  the left operand
  * @param callable $predicate                               the predicate function to be used
  * @return IOption\Type                                     an option containing the first object
  *                                                          satisfying the predicate, if any
  */
 public static function find(IOption\Type $xs, callable $predicate) : IOption\Type
 {
     if (IBool\Module::and_($xs->isDefined(), $predicate($xs->item(), IInt32\Type::zero()))->unbox()) {
         return $xs;
     }
     return IOption\Type::none();
 }
Ejemplo n.º 6
0
 /**
  * This method tests the "projectRight" method.
  */
 public function test_projectRight()
 {
     $p0 = IEither\Type::left(IInt32\Type::zero())->projectRight();
     $this->assertInstanceOf('\\Saber\\Data\\IEither\\Right\\Projection', $p0);
     $p1 = IEither\Type::right(IInt32\Type::zero())->projectRight();
     $this->assertInstanceOf('\\Saber\\Data\\IEither\\Right\\Projection', $p1);
 }
Ejemplo n.º 7
0
 /**
  * This method returns the latter value should the former value evaluates
  * to null.
  *
  * @access public
  * @static
  * @param IInt32\Type $x                                    the value to be evaluated
  * @param IInt32\Type $y                                    the default value
  * @return IInt32\Type                                      the result
  */
 public static function nvl(IInt32\Type $x = null, IInt32\Type $y = null) : IInt32\Type
 {
     return $x ?? $y ?? IInt32\Type::zero();
 }
Ejemplo n.º 8
0
 /**
  * This method tests the "item" related methods.
  */
 public function test_items()
 {
     $p0 = ILinkedList\Type::box2(IInt32\Type::zero(), IInt32\Type::one(), IInt32\Type::box(2));
     $this->assertSame(0, $p0->item(IInt32\Type::zero())->unbox());
     $this->assertSame(1, $p0->item(IInt32\Type::one())->unbox());
     $this->assertSame(2, $p0->item(IInt32\Type::box(2))->unbox());
     $this->assertSame(0, $p0->head()->unbox());
     $p1 = $p0->tail();
     $this->assertInstanceOf('\\Saber\\Data\\ILinkedList\\Type', $p1);
     $this->assertSame(array(1, 2), $p1->unbox(1));
 }
Ejemplo n.º 9
0
 /**
  * This method provides the data for testing that a value is converted to a string.
  *
  * @return array
  */
 public function data_toString()
 {
     $data = array(array(array(new Throwable\InvalidArgument\Exception('Message', array(), IInt32\Type::zero())), array('Saber\\Throwable\\InvalidArgument\\Exception [ 0 ]: Message ~ ')));
     return $data;
 }
Ejemplo n.º 10
0
 /**
  * This method provides the data for testing that a value is of a particular size.
  *
  * @return array
  */
 public function dataSize()
 {
     $data = array(array(array(), array(0)), array(array(IInt32\Type::zero()), array(1)), array(array(IInt32\Type::zero(), IInt32\Type::one(), IInt32\Type::box(2)), array(3)), array(array(IInt32\Type::zero(), IInt32\Type::zero(), IInt32\Type::zero()), array(1)), array(array(IInt32\Type::zero(), IInt32\Type::zero(), IInt32\Type::one(), IInt32\Type::one()), array(2)));
     return $data;
 }
Ejemplo n.º 11
0
 /**
  * This method provides the data for testing that a value is of a particular size.
  *
  * @return array
  */
 public function dataSize()
 {
     $data = array(array(array(), array(0)), array(array(ITuple\Type::box2(IString\Type::box('key0'), IInt32\Type::zero())), array(1)), array(array(ITuple\Type::box2(IString\Type::box('key0'), IInt32\Type::zero()), ITuple\Type::box2(IString\Type::box('key1'), IInt32\Type::one())), array(2)));
     return $data;
 }
Ejemplo n.º 12
0
 /**
  * This method provides the data for testing that a value is converted to a string.
  *
  * @return array
  */
 public function data_toString()
 {
     $data = array(array(array('Message', array(), IInt32\Type::zero()), array('Saber\\Throwable\\UnimplementedMethod\\Exception [ 0 ]: Message ~ ')));
     return $data;
 }
Ejemplo n.º 13
0
 /**
  * This method tests the creation of a constant function that wraps an object.
  */
 public function testConstant()
 {
     $constant = IFunction\Utilities::constant(IInt32\Type::zero());
     $this->assertInternalType('callable', $constant);
     $this->assertSame(IInt32\Type::zero(), $constant());
 }
Ejemplo n.º 14
0
 /**
  * This method returns whether the ratio is a whole number.
  *
  * @access public
  * @static
  * @param IRatio\Type $x                                    the ratio to be evaluated
  * @return IBool\Type                                       whether the ratio is a whole
  *                                                          number
  */
 public static function isInteger(IRatio\Type $x) : IBool\Type
 {
     return IBool\Module::or_(IInt32\Module::eq($x->numerator(), IInt32\Type::zero()), IInt32\Module::eq($x->denominator(), IInt32\Type::one()));
 }
Ejemplo n.º 15
0
 /**
  * This method tests the "nvl" method.
  */
 public function test_nvl()
 {
     $x = IInt32\Type::one();
     $y = IInt32\Type::zero();
     $z = IInt32\Module::nvl($x, $y);
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $z);
     $this->assertSame(1, $z->unbox());
     $z = IInt32\Module::nvl(null, $x);
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $z);
     $this->assertSame(1, $z->unbox());
     $z = IInt32\Module::nvl(null, null);
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $z);
     $this->assertSame(0, $z->unbox());
 }
Ejemplo n.º 16
0
 /**
  * This method tests the "item" related methods.
  */
 public function test_items()
 {
     $p0 = ITuple\Type::box2(IInt32\Type::zero(), IInt32\Type::one(), IInt32\Type::box(2));
     $this->assertSame(0, $p0->item(IInt32\Type::zero())->unbox());
     $this->assertSame(1, $p0->item(IInt32\Type::one())->unbox());
     $this->assertSame(2, $p0->item(IInt32\Type::box(2))->unbox());
     $this->assertSame(0, $p0->first()->unbox());
     $this->assertSame(1, $p0->second()->unbox());
 }
Ejemplo n.º 17
0
 /**
  * This method tests the "item" related methods.
  */
 public function test_items()
 {
     $p0 = IString\Type::box('012');
     $this->assertSame('0', $p0->item(IInt32\Type::zero())->unbox());
     $this->assertSame('1', $p0->item(IInt32\Type::one())->unbox());
     $this->assertSame('2', $p0->item(IInt32\Type::box(2))->unbox());
     $this->assertSame('0', $p0->head()->unbox());
     $p1 = $p0->tail();
     $this->assertInstanceOf('\\Saber\\Data\\IString\\Type', $p1);
     $this->assertSame('12', $p1->unbox());
 }
Ejemplo n.º 18
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;
 }
Ejemplo n.º 19
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());
 }
Ejemplo n.º 20
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;
 }
Ejemplo n.º 21
0
 /**
  * This method applies each item in this either to the subroutine function.
  *
  * @access public
  * @final
  * @param callable $subroutine                              the subroutine function to be used
  * @return IEither\Type                                     the either
  */
 public final function map(callable $subroutine) : IEither\Type
 {
     return $this->either->__isLeft() ? IEither\Type::left($subroutine($this->either->item(), IInt32\Type::zero())) : IEither\Type::right($this->either->projectRight()->item());
 }
Ejemplo n.º 22
0
 /**
  * This method provides the data for testing that a value is converted to a string.
  *
  * @return array
  */
 public function data_toString()
 {
     $data = array(array(array('Message', array(), IInt32\Type::zero()), array('Saber\\Throwable\\EmptyCollection\\Exception [ 0 ]: Message ~ ')));
     return $data;
 }
Ejemplo n.º 23
0
 /**
  * This method tests the "size" method.
  */
 public function test_size()
 {
     $p0 = IOption\Type::some(IInt32\Type::zero())->size();
     $e0 = 1;
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $p0);
     $this->assertSame($e0, $p0->unbox());
     $p1 = IOption\Type::none()->size();
     $e1 = 0;
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $p1);
     $this->assertSame($e1, $p1->unbox());
 }
Ejemplo n.º 24
0
 /**
  * This method tests the "foldRight" method.
  *
  * @dataProvider data_foldRight
  */
 public function test_foldRight(array $provided, array $expected)
 {
     $p0 = ILinkedList\Type::make($provided[0], '\\Saber\\Data\\IInt32\\Type');
     $p1 = $provided[1];
     $r0 = ILinkedList\Module::foldRight($p0, $p1, IInt32\Type::zero());
     $e0 = $expected[0];
     $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $r0);
     $this->assertSame($e0, $r0->unbox());
 }
Ejemplo n.º 25
0
 /**
  * This method returns an object with a "0" value.
  *
  * @access public
  * @static
  * @return IRatio\Type                                      the object
  */
 public static function zero()
 {
     if (!isset(static::$singletons[0])) {
         static::$singletons[0] = new IRatio\Type(IInt32\Type::zero(), IInt32\Type::one());
     }
     return static::$singletons[0];
 }
Ejemplo n.º 26
0
 /**
  * This method provides the data for testing the making of a boxed value.
  *
  * @return array
  */
 public function data_singleton()
 {
     $data = array(array(array(IRatio\Type::one()), array(IInt32\Type::one(), IInt32\Type::one())), array(array(IRatio\Type::zero()), array(IInt32\Type::zero(), IInt32\Type::one())), array(array(IRatio\Type::negative()), array(IInt32\Type::negative(), IInt32\Type::one())));
     return $data;
 }
Ejemplo n.º 27
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;
 }