diff --git a/docs/pages/api.rst b/docs/pages/api.rst index 7daee3999..cdb050eb5 100644 --- a/docs/pages/api.rst +++ b/docs/pages/api.rst @@ -822,25 +822,6 @@ Signature: ``Collection::limit(int $limit);`` $collection = Collection::iterate($fibonacci) ->limit(10); -loop -~~~~ - -Loop over the values of the collection indefinitely, in a cyclic way. - -Interface: `Loopable`_ - -Signature: ``Collection::loop();`` - -.. code-block:: php - - $diceData = range(1, 6); - - // Simulate a dice throw. - $randomDiceValue = Collection::with($data) - ->loop() - ->limit(random_int(0, 1000)) - ->last(); - map ~~~ @@ -1553,7 +1534,6 @@ Signature: ``Collection::zip(iterable ...$iterables);`` .. _Keysable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Keysable.php .. _Lastable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Lastable.php .. _Limitable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Limitable.php -.. _Loopable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Loopable.php .. _Mapable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Mapable.php .. _Mergeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Mergeable.php .. _Normalizeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Normalizeable.php diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index 41fc7edb0..f45e018f8 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -477,60 +477,25 @@ public function it_can_count_its_items(): void public function it_can_cycle(): void { - $iterable = ['a' => '1', 'b' => '2', 'c' => '3']; - - $this::fromIterable($iterable) - ->cycle() - ->shouldIterateAs([]); - - $generator = static function () { - yield 'a' => '1'; - - yield 'b' => '2'; - - yield 'c' => '3'; - }; - - $this::fromIterable($iterable) - ->cycle(3) - ->shouldIterateAs($generator()); - - $generator = static function () { - yield 'a' => '1'; - - yield 'b' => '2'; - - yield 'c' => '3'; - - yield 'a' => '1'; - - yield 'b' => '2'; - - yield 'c' => '3'; - }; - - $this::fromIterable($iterable) - ->cycle(6) - ->shouldIterateAs($generator()); - - $generator = static function () { - yield 'a' => '1'; + $generator = static function (): Generator { + yield 0 => 1; - yield 'b' => '2'; + yield 1 => 2; - yield 'c' => '3'; + yield 2 => 3; - yield 'a' => '1'; + yield 0 => 1; - yield 'b' => '2'; + yield 1 => 2; - yield 'c' => '3'; + yield 2 => 3; - yield 'a' => '1'; + yield 0 => 1; }; - $this::fromIterable($iterable) - ->cycle(7) + $this::fromIterable(range(1, 3)) + ->cycle() + ->limit(7) ->shouldIterateAs($generator()); } @@ -1256,28 +1221,6 @@ public function it_can_limit(): void ->shouldIterateAs($input); } - public function it_can_loop(): void - { - $generator = static function (): Generator { - yield 0 => 1; - - yield 1 => 2; - - yield 2 => 3; - - yield 0 => 1; - - yield 1 => 2; - - yield 2 => 3; - }; - - $this::fromIterable(range(1, 3)) - ->loop() - ->limit(6) - ->shouldIterateAs($generator()); - } - public function it_can_map(): void { $input = array_combine(range('A', 'E'), range('A', 'E')); diff --git a/src/Collection.php b/src/Collection.php index 3393b34cc..f0651aee0 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -51,7 +51,6 @@ use loophp\collection\Operation\Keys; use loophp\collection\Operation\Last; use loophp\collection\Operation\Limit; -use loophp\collection\Operation\Loop; use loophp\collection\Operation\Map; use loophp\collection\Operation\Merge; use loophp\collection\Operation\Normalize; @@ -289,9 +288,9 @@ public function count(): int return iterator_count($this); } - public function cycle(int $length = 0): CollectionInterface + public function cycle(): CollectionInterface { - return $this->run(Cycle::of()($length)); + return $this->run(Cycle::of()); } public function diff(...$values): CollectionInterface @@ -506,11 +505,6 @@ public function limit(int $limit = -1, int $offset = 0): CollectionInterface return $this->run(Limit::of()($limit)($offset)); } - public function loop(): CollectionInterface - { - return $this->run(Loop::of()); - } - public function map(callable ...$callbacks): CollectionInterface { return $this->run(Map::of()(...$callbacks)); diff --git a/src/Contract/Collection.php b/src/Contract/Collection.php index 7a1192a82..f4cb24b95 100644 --- a/src/Contract/Collection.php +++ b/src/Contract/Collection.php @@ -45,7 +45,6 @@ use loophp\collection\Contract\Operation\Keysable; use loophp\collection\Contract\Operation\Lastable; use loophp\collection\Contract\Operation\Limitable; -use loophp\collection\Contract\Operation\Loopable; use loophp\collection\Contract\Operation\Mapable; use loophp\collection\Contract\Operation\Mergeable; use loophp\collection\Contract\Operation\Normalizeable; @@ -127,7 +126,6 @@ * @template-extends Keysable * @template-extends Lastable * @template-extends Limitable - * @template-extends Loopable * @template-extends Mapable * @template-extends Mergeable * @template-extends Normalizeable @@ -206,7 +204,6 @@ interface Collection extends Keysable, Lastable, Limitable, - Loopable, Mapable, Mergeable, Normalizeable, diff --git a/src/Contract/Operation/Cycleable.php b/src/Contract/Operation/Cycleable.php index b05ab6833..f3464eaa5 100644 --- a/src/Contract/Operation/Cycleable.php +++ b/src/Contract/Operation/Cycleable.php @@ -14,7 +14,7 @@ interface Cycleable { /** - * @psalm-return \loophp\collection\Contract\Collection + * @psalm-return static */ - public function cycle(int $length = 0): Collection; + public function cycle(): Collection; } diff --git a/src/Contract/Operation/Loopable.php b/src/Contract/Operation/Loopable.php deleted file mode 100644 index 49914f4ef..000000000 --- a/src/Contract/Operation/Loopable.php +++ /dev/null @@ -1,20 +0,0 @@ - - */ - public function loop(): Collection; -} diff --git a/src/Operation/Chunk.php b/src/Operation/Chunk.php index 0f6b535f9..a1440a401 100644 --- a/src/Operation/Chunk.php +++ b/src/Operation/Chunk.php @@ -36,7 +36,7 @@ static function (int ...$sizes): Closure { */ static function (Iterator $iterator) use ($sizes): Generator { /** @psalm-var Iterator $sizesIterator */ - $sizesIterator = Loop::of()(new ArrayIterator($sizes)); + $sizesIterator = Cycle::of()(new ArrayIterator($sizes)); $values = []; diff --git a/src/Operation/Cycle.php b/src/Operation/Cycle.php index cff5226d2..1e6445716 100644 --- a/src/Operation/Cycle.php +++ b/src/Operation/Cycle.php @@ -8,7 +8,6 @@ use Generator; use InfiniteIterator; use Iterator; -use LimitIterator; /** * @psalm-template TKey @@ -18,28 +17,18 @@ final class Cycle extends AbstractOperation { /** - * @psalm-return Closure(int): Closure(Iterator): Generator + * @psalm-return Closure(Iterator): Generator */ public function __invoke(): Closure { - return static function (int $length): Closure { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($length): Generator { - if (0 === $length) { - return yield from []; - } - - return yield from new LimitIterator( - new InfiniteIterator($iterator), - 0, - $length - ); - }; - }; + return + /** + * @psalm-param Iterator $iterator + * + * @psalm-return Generator + */ + static function (Iterator $iterator): Generator { + return yield from new InfiniteIterator($iterator); + }; } } diff --git a/src/Operation/Loop.php b/src/Operation/Loop.php deleted file mode 100644 index fd106e03c..000000000 --- a/src/Operation/Loop.php +++ /dev/null @@ -1,34 +0,0 @@ -): Generator - */ - public function __invoke(): Closure - { - return - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Iterator - */ - static function (Iterator $iterator): Generator { - return yield from new InfiniteIterator($iterator); - }; - } -} diff --git a/src/Operation/Window.php b/src/Operation/Window.php index bcf351526..97a6ea6c1 100644 --- a/src/Operation/Window.php +++ b/src/Operation/Window.php @@ -31,7 +31,7 @@ public function __invoke(): Closure */ static function (Iterator $iterator) use ($lengths): Generator { /** @psalm-var Iterator $lengths */ - $lengths = Loop::of()(new ArrayIterator($lengths)); + $lengths = Cycle::of()(new ArrayIterator($lengths)); for ($i = 0; iterator_count($iterator) > $i; ++$i) { /** @psalm-var Generator $slice */