Example #1
0
 /**
  * This method returns an option containing the value paired with the lookup key x.
  *
  * @access public
  * @static
  * @param IArrayList\Type $xss                              the left operand
  * @param Core\Equality\Type $x                             the key being looked up
  * @return IOption\Type                                     an option containing the associated
  *                                                          value
  * @throws Throwable\UnexpectedValue\Exception              indicates that the list is not
  *                                                          associative
  */
 public static function lookup(IArrayList\Type $xss, Core\Equality\Type $x) : IOption\Type
 {
     $xssi = IArrayList\Module::iterator($xss);
     foreach ($xssi as $i => $xs) {
         if (!ITuple\Module::isPair($xs)->unbox()) {
             throw new Throwable\UnexpectedValue\Exception('Unable to process tuple. Expected a length of "2", but got a length of ":length".', array(':length' => $xs->__length()));
         }
         if ($x->__eq(ITuple\Module::first($xs))) {
             return IOption\Type::some(ITuple\Module::second($xs));
         }
     }
     return IOption\Type::none();
 }
Example #2
0
 /**
  * This method sets the procedure that will be executed should "y" equal "x".
  *
  * @access public
  * @final
  * @param Core\Equality\Type $y                             the object to be evaluated
  *                                                          against
  * @param callable $procedure                               the procedure to be executed
  * @return Control\Choice\Type                              a reference to the next choice
  *                                                          monad node
  */
 public final function when(Core\Equality\Type $y, callable $procedure) : Control\Choice\Type
 {
     $this->predicate = function ($x) use($y, $procedure) {
         if ($y->__eq($x)) {
             IUnit\Type::covariant($procedure($x));
             return true;
         }
         return false;
     };
     return Control\Choice\Type::cons($this->x, $this);
 }
Example #3
0
 /**
  * This method returns an option containing the value paired with the lookup key x.
  *
  * @access public
  * @static
  * @param ILinkedList\Type $xss                             the left operand
  * @param Core\Equality\Type $x                             the key being looked up
  * @return IOption\Type                                     an option containing the associated
  *                                                          value
  * @throws Throwable\UnexpectedValue\Exception              indicates that the list is not
  *                                                          associative
  */
 public static function lookup(ILinkedList\Type $xss, Core\Equality\Type $x) : IOption\Type
 {
     if ($xss->__isEmpty()) {
         return IOption\Type::none();
     }
     $xs = $xss->head();
     if (!ITuple\Module::isPair($xs)->unbox()) {
         throw new Throwable\UnexpectedValue\Exception('Unable to process tuple. Expected a length of "2", but got a length of ":length".', array(':length' => $xs->__length()));
     }
     if ($x->__eq(ITuple\Module::first($xs))) {
         return IOption\Type::some(ITuple\Module::second($xs));
     }
     return ILinkedList\Module::lookup($xss->tail(), $x);
 }