/** * 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; }
/** * This method computes the nth fibonacci number. * * @access public * @static * @param IInt32\Type $n the operand * @return IInt32\Type the result */ public static function fibonacci(IInt32\Type $n) : IInt32\Type { return IInt32\Module::le($n, IInt32\Type::one())->unbox() ? $n : IInt32\Module::add(IInt32\Module::fibonacci(IInt32\Module::decrement($n)), IInt32\Module::fibonacci(IInt32\Module::subtract($n, IInt32\Type::box(2)))); }
/** * This method creates a list of "n" length with every item set to the given object. * * @access public * @static * @param Core\Type $x the object to be replicated * @param IInt32\Type $n the number of times to replicate * @return ILinkedList\Type the collection */ public static function replicate(Core\Type $x, IInt32\Type $n) : ILinkedList\Type { if ($n->unbox() <= 0) { return ILinkedList\Type::nil(); } return ILinkedList\Type::cons($x, ILinkedList\Type::replicate($x, IInt32\Module::decrement($n))); }
/** * This method tests the "decrement" method. * * @dataProvider data_decrement */ public function test_decrement(array $provided, array $expected) { $p0 = IInt32\Module::decrement(IInt32\Type::box($provided[0])); $e0 = $expected[0]; $this->assertInstanceOf('\\Saber\\Data\\IInt32\\Type', $p0); $this->assertEquals($e0, $p0->unbox()); }
/** * This method returns the last item in this list. * * @access public * @static * @param IArrayList\Type $xs the left operand * @return mixed the last item in this linked * list */ public static function last(IArrayList\Type $xs) { return $xs->item(IInt32\Module::decrement($xs->length())); }
/** * This method returns the first "n" items in the list. * * @access public * @static * @param ILinkedList\Type $xs the left operand * @param IInt32\Type $n the number of items to take * @return ILinkedList\Type the list */ public static function take(ILinkedList\Type $xs, IInt32\Type $n) : ILinkedList\Type { if ($n->unbox() <= 0 || $xs->__isEmpty()) { return ILinkedList\Type::nil(); } return ILinkedList\Type::cons($xs->head(), ILinkedList\Module::take($xs->tail(), IInt32\Module::decrement($n))); }