/** * This method returns each item in this string until the predicate fails. * * @access public * @static * @param IString\Type $xs the left operand * @param callable $predicate the predicate function to be used * @return IString\Type the string */ public static function takeWhile(IString\Type $xs, callable $predicate) : IString\Type { $buffer = ''; $length = $xs->length(); for ($i = IInt32\Type::zero(); IInt32\Module::lt($i, $length)->unbox(); $i = IInt32\Module::increment($i)) { $x = $xs->item($i); if (!$predicate($x, $i)->unbox()) { break; } $buffer .= $x->unbox(); } return IString\Type::box($buffer); }
/** * 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()); }
/** * 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; }
/** * This method tests the "increment" method. * * @dataProvider data_increment */ public function test_increment(array $provided, array $expected) { $p0 = IInt32\Module::increment(IInt32\Type::box($provided[0])); $e0 = $expected[0]; $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $p0); $this->assertEquals($e0, $p0->unbox()); }
/** * This method applies each item in this hash set to the subroutine function. * * @access public * @static * @param IHashMap\Type $xs the hash map * @param callable $subroutine the subroutine function to be used * @return IHashMap\Type the hash map */ public static function map(IHashMap\Type $xs, callable $subroutine) : IHashMap\Type { $zs = IHashMap\Type::empty_(); $xi = IHashMap\Module::iterator($xs); $i = IInt32\Type::zero(); foreach ($xi as $k => $v) { $entry = ITuple\Type::box2($k, $v); $zs = IHashMap\Module::putEntry($zs, $subroutine($entry, $i)); $i = IInt32\Module::increment($i); } return $zs; }
/** * 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()); }
/** * 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; }