Exemple #1
0
 /**
  * This method unsubscribes a listener.
  *
  * @access public
  * @static
  * @param IHashMap\Type $xs                                 the left operand
  * @param ITuple\Type $entry                                a tuple containing the key and listener
  * @return IHashMap\Type                                    the map
  */
 public static function unsubscribe(IHashMap\Type $xs, ITuple\Type $entry) : IHashMap\Type
 {
     $k = $entry->first();
     if ($xs->__hasKey($k)) {
         $ys = ILinkedList\Module::delete($xs->item($k), $entry->second());
         $zs = IHashMap\Module::putEntry($xs, ITuple\Type::box2($k, $ys));
         return $zs;
     }
     return $xs;
 }
 /**
  * This method tests the "split" method.
  *
  * @dataProvider data_split
  */
 public function test_split(array $provided, array $expected)
 {
     if (is_array($provided[1])) {
         $p0 = IRegex\Module::split(IRegex\Type::box($provided[0]), ITuple\Type::box2(IString\Type::box($provided[1][0]), IInt32\Type::box($provided[1][1])));
     } else {
         $p0 = IRegex\Module::split(IRegex\Type::box($provided[0]), IString\Type::box($provided[1]));
     }
     $e0 = $expected[0];
     $this->assertInstanceOf('\\Saber\\Data\\IArrayList\\Type', $p0);
     $this->assertSame($e0, $p0->unbox(1));
 }
Exemple #3
0
 /**
  * This method adds the item with the specified key to the collection (if it doesn't
  * already exist).
  *
  * @access public
  * @final
  * @param Core\Type $key                                    the key to associate with
  *                                                          the item
  * @param Core\Type $item                                   the item to be stored
  * @return IHashMap\Type                                    the hash map
  *
  * @see http://stackoverflow.com/questions/4980757/how-do-hashtables-deal-with-collisions
  */
 public final function putEntry(Core\Type $key, Core\Type $item) : IHashMap\Type
 {
     $hashCode = $key->__hashCode();
     if (array_key_exists($hashCode, $this->value)) {
         $bucket = $this->value[$hashCode];
         foreach ($bucket as $entry) {
             if ($entry->first()->__eq($key)) {
                 return $this;
             }
         }
     }
     $this->value[$hashCode][] = ITuple\Type::box2($key, $item);
     return $this;
 }
Exemple #4
0
 /**
  * This method returns a tuple where the first item contains the first "n" characters
  * in the string and the second item contains the remainder.
  *
  * @access public
  * @static
  * @param IString\Type $xs                                  the string
  * @param IInt32\Type $n                                    the number of characters to take
  * @return ITuple\Type                                      the tuple
  */
 public static function split(IString\Type $xs, IInt32\Type $n) : ITuple\Type
 {
     return ITuple\Type::box2(IString\Module::take($xs, $n), IString\Module::drop($xs, $n));
 }
Exemple #5
0
 /**
  * This method returns a value as a boxed object.  A value is typically a PHP typed
  * array.  It is considered type-safe.
  *
  * @access public
  * @static
  * @param mixed ...$xs                                      the value(s) to be boxed
  * @return ITuple\Type                                      the boxed object
  * @throws Throwable\InvalidArgument\Exception              indicates an invalid argument
  */
 public static function make2(...$xs) : ITuple\Type
 {
     return ITuple\Type::make($xs);
 }
 /**
  * This method tests the "isPair" method.
  *
  * @dataProvider data_isPair
  */
 public function test_isPair(array $provided, array $expected)
 {
     $p0 = ITuple\Module::isPair(ITuple\Type::make($provided[0]));
     $e0 = $expected[0];
     $this->assertInstanceOf('\\Saber\\Data\\IBool\\Type', $p0);
     $this->assertSame($e0, $p0->unbox());
 }
Exemple #7
0
 /**
  * This method tests the "toString" method.
  *
  * @dataProvider data_toString
  */
 public function test_toString(array $provided, array $expected)
 {
     $this->markTestIncomplete();
     $p0 = ITuple\Type::make($provided[0])->toString();
     $e0 = $expected[0];
     $this->assertInstanceOf('\\Saber\\Data\\IString\\Type', $p0);
     $this->assertSame($e0, $p0->unbox());
 }
 /**
  * This method tests the "sequence" method.
  *
  * @dataProvider data_sequence
  */
 public function test_sequence(array $provided, array $expected)
 {
     if (is_array($provided[1])) {
         $p0 = IInt32\Module::sequence(IInt32\Type::box($provided[0]), ITuple\Type::box(array_map(function (int $item) : IInt32\Type {
             return IInt32\Type::box($item);
         }, $provided[1])));
         $e0 = $expected[0];
     } else {
         $p0 = IInt32\Module::sequence(IInt32\Type::box($provided[0]), IInt32\Type::box($provided[1]));
         $e0 = $expected[0];
     }
     $this->assertInstanceOf('\\Saber\\Data\\IArrayList\\Type', $p0);
     $this->assertEquals($e0, $p0->unbox(1));
 }
Exemple #9
0
 /**
  * This method adds the item with the specified key to the hash map (if it doesn't
  * already exist).
  *
  * @access public
  * @static
  * @param IHashMap\Type $xs                                 the hash map
  * @param ITuple\Type $entry                                the key/value pair to be put
  *                                                          in the hash map
  * @return IHashMap\Type                                    the hash map
  */
 public static function putEntry(IHashMap\Type $xs, ITuple\Type $entry) : IHashMap\Type
 {
     $zs = IHashMap\Type::box($xs->unbox());
     $zs->putEntry($entry->first(), $entry->second());
     return $zs;
 }
Exemple #10
0
 /**
  * This method returns a new string after replacing any matches with the specified
  * replacement.
  *
  * @access public
  * @static
  * @param IRegex\Type $x                                    the regular expression
  * @param ITuple\Type $ys                                   a tuple containing the replacement
  *                                                          and the object that is the subject
  * @return IString\Type                                     the string after being processed
  */
 public static function replace(IRegex\Type $x, ITuple\Type $ys) : IString\Type
 {
     return IString\Type::box(preg_replace($x->unbox(), $ys->first()->__toString(), $ys->second()->__toString()));
 }
Exemple #11
0
 /**
  * This method returns a pair of hash sets: those items that satisfy the predicate and
  * those items that do not satisfy the predicate.
  *
  * @access public
  * @static
  * @param IHashSet\Type $xs                                 the hash set to be partitioned
  * @param callable $predicate                               the predicate function to be used
  * @return ITuple\Type                                      the results
  */
 public static function partition(IHashSet\Type $xs, callable $predicate) : ITuple\Type
 {
     $passed = IHashSet\Type::empty_();
     $failed = IHashSet\Type::empty_();
     $xi = IHashSet\Module::iterator($xs);
     foreach ($xi as $i => $x) {
         if ($predicate($x, $i)->unbox()) {
             $passed->putItem($x);
         } else {
             $failed->putItem($x);
         }
     }
     return ITuple\Type::box2($passed, $failed);
 }
Exemple #12
0
 /**
  * This method evaluates whether the tuple is a pair.
  *
  * @access public
  * @static
  * @param ITuple\Type $x                                    the object to be evaluated
  * @return IBool\Type                                       whether the tuple is a pair
  */
 public static function isPair(ITuple\Type $x) : IBool\Type
 {
     return $x->isPair();
 }
Exemple #13
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;
 }
Exemple #14
0
 /**
  * This method returns a new list of tuple pairings.
  *
  * @access public
  * @static
  * @param IArrayList\Type $xs                               the left operand
  * @param IArrayList\Type $ys                               the right operand
  * @return IArrayList\Type                                  a new list of tuple pairings
  */
 public static function zip(IArrayList\Type $xs, IArrayList\Type $ys) : IArrayList\Type
 {
     $buffer = array();
     $length = IInt32\Module::min($xs->length(), $ys->length());
     for ($i = IInt32\Type::zero(); IInt32\Module::lt($i, $length)->unbox(); $i = IInt32\Module::increment($i)) {
         $buffer[] = ITuple\Type::box2($xs->item($i), $ys->item($i));
     }
     return IArrayList\Type::box($buffer);
 }
Exemple #15
0
 /**
  * This method returns a new list of tuple pairings.
  *
  * @access public
  * @static
  * @param ILinkedList\Type $xs                              the left operand
  * @param ILinkedList\Type $ys                              the right operand
  * @return ILinkedList\Type                                 a new list of tuple pairings
  */
 public static function zip(ILinkedList\Type $xs, ILinkedList\Type $ys) : ILinkedList\Type
 {
     $start = ILinkedList\Type::nil();
     $tail = null;
     for ($as = $xs, $bs = $ys; !$as->__isEmpty() && !$bs->__isEmpty(); $as = $as->tail(), $bs = $bs->tail()) {
         $tuple = ITuple\Type::box2($as->head(), $bs->head());
         $cons = ILinkedList\Type::cons($tuple);
         if ($tail !== null) {
             $tail->tail = $cons;
         } else {
             $start = $cons;
         }
         $tail = $cons;
     }
     return $start;
 }