Ejemplo n.º 1
0
 /**
  * This method (aka "every" and "forall") iterates over the items in the list, yielding
  * each item to the predicate function, or fails the truthy test.  Opposite of "none".
  *
  * @access public
  * @static
  * @param IString\Type $xs                                  the left operand
  * @param callable $predicate                               the predicate function to be used
  * @return IBool\Type                                       whether each item passed the
  *                                                          truthy test
  */
 public static function all(IString\Type $xs, callable $predicate) : IBool\Type
 {
     $xi = IString\Module::iterator($xs);
     foreach ($xi as $i => $x) {
         if (!$predicate($x, $i)->unbox()) {
             return IBool\Type::false();
         }
     }
     return IBool\Type::true();
     // yes, empty returns "true"
 }
Ejemplo n.º 2
0
 /**
  * This method (aka "exists" or "some") returns whether some of the items in the list passed the truthy
  * test.
  *
  * @access public
  * @static
  * @param IHashMap\Type $xs                                 the hash map
  * @param callable $predicate                               the predicate function to be used
  * @return IBool\Type                                       whether some of the items
  *                                                          passed the truthy test
  */
 public static function any(IHashMap\Type $xs, callable $predicate) : IBool\Type
 {
     $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()) {
             return IBool\Type::true();
         }
         $i = IInt32\Module::increment($i);
     }
     return IBool\Type::false();
 }
Ejemplo n.º 3
0
 /**
  * This method evaluates whether the specified object is identical to the current object.
  *
  * @access public
  * @static
  * @param IOption\Type $xs                                  the left operand
  * @param Core\Type $ys                                     the object to be evaluated
  * @return IBool\Type                                       whether the specified object is identical
  *                                                          to the current object
  */
 public static function id(IOption\Type $xs, Core\Type $ys) : IBool\Type
 {
     if ($ys !== null) {
         if ($xs->__typeOf() === $ys->__typeOf()) {
             if ($ys instanceof IOption\Some\Type) {
                 $x = $xs->item();
                 $y = $ys->item();
                 if ($x === null) {
                     return IBool\Type::box($y === null);
                 } else {
                     if ($x instanceof Core\Equality\Type) {
                         return $x->id($y);
                     }
                 }
                 return IBool\Type::box(spl_object_hash($x) === spl_object_hash($y));
             } else {
                 if ($ys instanceof IOption\None\Type) {
                     return IBool\Type::true();
                 }
             }
         }
     }
     return IBool\Type::false();
 }
Ejemplo n.º 4
0
 /**
  * This method tests the "xor_" method.
  */
 public function test_xor_()
 {
     $x = IBool\Type::true();
     $y = IBool\Type::false();
     $z = IBool\Module::xor_($x, $y);
     $this->assertInstanceOf('\\Saber\\Data\\IBool\\Type', $z);
     $this->assertSame(true, $z->unbox());
     $z = IBool\Module::xor_($x, $x);
     $this->assertInstanceOf('\\Saber\\Data\\IBool\\Type', $z);
     $this->assertSame(false, $z->unbox());
     $z = IBool\Module::xor_($y, $x);
     $this->assertInstanceOf('\\Saber\\Data\\IBool\\Type', $z);
     $this->assertSame(true, $z->unbox());
     $z = IBool\Module::xor_($y, $y);
     $this->assertInstanceOf('\\Saber\\Data\\IBool\\Type', $z);
     $this->assertSame(false, $z->unbox());
 }
Ejemplo n.º 5
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 IBool\Type                                       the boxed object
  */
 public static function make($value) : IBool\Type
 {
     if (is_string($value) && in_array(strtolower($value), array('false', 'f', 'no', 'n', '0', 'null', 'nil'))) {
         return IBool\Type::false();
     }
     return $value ? IBool\Type::true() : IBool\Type::false();
 }
Ejemplo n.º 6
0
 /**
  * This method returns whether the second hash set is a superset of the first hash set.
  *
  * @access public
  * @static
  * @param IHashSet\Type $xs                                 the first hash set
  * @param IHashSet\Type $ys                                 the second hash set
  * @return IBool\Type                                       whether the second hash set is a
  *                                                          superset of the first hash set
  */
 public static function isSuperset(IHashSet\Type $xs, IHashSet\Type $ys) : IBool\Type
 {
     $xi = IHashSet\Module::iterator($xs);
     foreach ($xi as $x) {
         if (!$ys->__hasItem($x)) {
             return IBool\Type::false();
         }
     }
     return IBool\Type::true();
 }
Ejemplo n.º 7
0
 /**
  * This method returns an array list containing only unique items from the specified
  * array list (i.e. duplicates are removed).
  *
  * @access public
  * @static
  * @param IArrayList\Type $xs                               the array list to be processed
  * @return IArrayList\Type                                  an array list with the duplicates
  *                                                          removed
  */
 public static function nub(IArrayList\Type $xs) : IArrayList\Type
 {
     $zs = IHashSet\Type::empty_();
     return IArrayList\Module::filter($xs, function (Core\Type $x, IInt32\Type $i) use($zs) : IBool\Type {
         if ($zs->__hasItem($x)) {
             return IBool\Type::false();
         }
         $zs->putItem($x);
         return IBool\Type::true();
     });
 }
Ejemplo n.º 8
0
 /**
  * This method tests the "singletons" methods.
  */
 public function test_singletons()
 {
     $p0 = IBool\Type::true();
     $e0 = IBool\Type::true();
     $this->assertInstanceOf('\\Saber\\Data\\IBool\\Type', $p0);
     $this->assertSame($e0->__hashCode(), $p0->__hashCode());
     $p1 = $p0->unbox();
     $e1 = true;
     $this->assertInternalType('boolean', $p1);
     $this->assertSame($e1, $p1);
     $p2 = IBool\Type::false();
     $e2 = IBool\Type::false();
     $this->assertInstanceOf('\\Saber\\Data\\IBool\\Type', $p2);
     $this->assertSame($e2->__hashCode(), $p2->__hashCode());
     $p3 = $p2->unbox();
     $e3 = false;
     $this->assertInternalType('boolean', $p3);
     $this->assertSame($e3, $p3);
 }
Ejemplo n.º 9
0
 /**
  * This method evaluates whether the left operand is identical to the right operand.
  *
  * @access public
  * @static
  * @param ILinkedList\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(ILinkedList\Type $xs, Core\Type $ys) : IBool\Type
 {
     // ===
     if ($ys !== null && $xs->__typeOf() == $ys->typeOf()) {
         for ($as = $xs, $bs = $ys; !$as->__isEmpty() && !$bs->__isEmpty(); $as = $as->tail(), $bs = $bs->tail()) {
             $r = $as->head()->id($bs->head());
             if (!$r->unbox()) {
                 return $r;
             }
         }
         $x_length = $xs->__length();
         $y_length = $ys->__length();
         if ($x_length == $y_length) {
             return IBool\Type::true();
         }
     }
     return IBool\Type::false();
 }