Skip to content

Commit

Permalink
Replace the Loop operation with the Cycle operation.
Browse files Browse the repository at this point in the history
Loop and Cycle operations were almost the same, it was a kind of nonsense to keep them both.

Loop operation has been deleted in favor of Cycle.
  • Loading branch information
drupol committed Sep 1, 2020
1 parent 58eeaa0 commit 2189649
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 178 deletions.
20 changes: 0 additions & 20 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~

Expand Down Expand Up @@ -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
Expand Down
79 changes: 11 additions & 68 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down Expand Up @@ -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'));
Expand Down
10 changes: 2 additions & 8 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
3 changes: 0 additions & 3 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,7 +126,6 @@
* @template-extends Keysable<TKey, T>
* @template-extends Lastable<TKey, T>
* @template-extends Limitable<TKey, T>
* @template-extends Loopable<TKey, T>
* @template-extends Mapable<TKey, T>
* @template-extends Mergeable<TKey, T>
* @template-extends Normalizeable<TKey, T>
Expand Down Expand Up @@ -206,7 +204,6 @@ interface Collection extends
Keysable,
Lastable,
Limitable,
Loopable,
Mapable,
Mergeable,
Normalizeable,
Expand Down
4 changes: 2 additions & 2 deletions src/Contract/Operation/Cycleable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
interface Cycleable
{
/**
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
* @psalm-return static<TKey, T>
*/
public function cycle(int $length = 0): Collection;
public function cycle(): Collection;
}
20 changes: 0 additions & 20 deletions src/Contract/Operation/Loopable.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Operation/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static function (int ...$sizes): Closure {
*/
static function (Iterator $iterator) use ($sizes): Generator {
/** @psalm-var Iterator<int, int> $sizesIterator */
$sizesIterator = Loop::of()(new ArrayIterator($sizes));
$sizesIterator = Cycle::of()(new ArrayIterator($sizes));

$values = [];

Expand Down
31 changes: 10 additions & 21 deletions src/Operation/Cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Generator;
use InfiniteIterator;
use Iterator;
use LimitIterator;

/**
* @psalm-template TKey
Expand All @@ -18,28 +17,18 @@
final class Cycle extends AbstractOperation
{
/**
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return static function (int $length): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
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<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator): Generator {
return yield from new InfiniteIterator($iterator);
};
}
}
34 changes: 0 additions & 34 deletions src/Operation/Loop.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Operation/Window.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __invoke(): Closure
*/
static function (Iterator $iterator) use ($lengths): Generator {
/** @psalm-var Iterator<int, int> $lengths */
$lengths = Loop::of()(new ArrayIterator($lengths));
$lengths = Cycle::of()(new ArrayIterator($lengths));

for ($i = 0; iterator_count($iterator) > $i; ++$i) {
/** @psalm-var Generator<TKey, T> $slice */
Expand Down

0 comments on commit 2189649

Please sign in to comment.