From 0c6b01ae505c00baebe9cfc53c11ec698c3a39d4 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Mon, 7 Sep 2020 17:30:25 +0200 Subject: [PATCH] Rename Skip into Drop. --- docs/pages/api.rst | 30 +++++++++---------- spec/loophp/collection/CollectionSpec.php | 24 +++++++-------- src/Collection.php | 12 ++++---- src/Contract/Collection.php | 6 ++-- .../Operation/{Skipable.php => Dropable.php} | 4 +-- src/Operation/{Skip.php => Drop.php} | 2 +- src/Operation/Slice.php | 2 +- src/Operation/Tail.php | 2 +- 8 files changed, 41 insertions(+), 41 deletions(-) rename src/Contract/Operation/{Skipable.php => Dropable.php} (84%) rename src/Operation/{Skip.php => Drop.php} (95%) diff --git a/docs/pages/api.rst b/docs/pages/api.rst index c89aa2dbc..f6e19796c 100644 --- a/docs/pages/api.rst +++ b/docs/pages/api.rst @@ -463,6 +463,20 @@ Signature: ``Collection::distinct();`` $collection = Collection::with(['a', 'b', 'c', 'd', 'a']) ->distinct() +drop +~~~~ + +Drop the n first items of the collection. + +Interface: `Dropable`_ + +Signature: ``Collection::drop(int ...$counts);`` + +.. code-block:: php + + Collection::fromIterable(range(10, 20)) + ->drop(2); // [12,13,14,15,16,17,18,19,20] + dropWhile ~~~~~~~~~ @@ -1272,20 +1286,6 @@ Signature: ``Collection::since(callable ...$callbacks);`` print_r($collection); -skip -~~~~ - -Skip the n items of a collection. - -Interface: `Skipable`_ - -Signature: ``Collection::skip(int ...$counts);`` - -.. code-block:: php - - $collection = Collection::with(range(10, 20)) - ->skip(2); - slice ~~~~~ @@ -1577,6 +1577,7 @@ Signature: ``Collection::zip(iterable ...$iterables);`` .. _Diffable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Diffable.php .. _Diffkeysable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Diffkeysable.php .. _Distinctable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Distinctable.php +.. _Dropable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Dropable.php .. _DropWhileable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/DropWhileable.php .. _Explodeable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Explodeable.php .. _Falsyable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Falsyable.php @@ -1621,7 +1622,6 @@ Signature: ``Collection::zip(iterable ...$iterables);`` .. _Reductionable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Reductionable.php .. _Reverseable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Reverseable.php .. _Scaleable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Scaleable.php -.. _Skipable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Skipable.php .. _Sinceable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Sinceable.php .. _Sliceable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Sliceable.php .. _Sortable: https://github.com/loophp/collection/blob/master/src/Contract/Operation/Sortable.php diff --git a/spec/loophp/collection/CollectionSpec.php b/spec/loophp/collection/CollectionSpec.php index d9b784c04..1e83bf3df 100644 --- a/spec/loophp/collection/CollectionSpec.php +++ b/spec/loophp/collection/CollectionSpec.php @@ -543,6 +543,17 @@ public function it_can_do_the_cartesian_product(): void ->shouldIterateAs([0 => ['A', 1], 1 => ['A', 2], 2 => ['B', 1], 3 => ['B', 2], 4 => ['C', 1], 5 => ['C', 2]]); } + public function it_can_drop(): void + { + $this::fromIterable(range('A', 'F')) + ->drop(3) + ->shouldIterateAs([3 => 'D', 4 => 'E', 5 => 'F']); + + $this::fromIterable(range('A', 'F')) + ->drop(3, 3) + ->shouldIterateAs([]); + } + public function it_can_dropWhile(): void { $isSmallerThanThree = static function ($value) { @@ -1568,7 +1579,7 @@ public function it_can_reverse(): void ->shouldIterateAs([5 => 'F', 4 => 'E', 3 => 'D', 2 => 'C', 1 => 'B', 0 => 'A']); $this::fromIterable(range('A', 'F')) - ->skip(3, 3) + ->drop(3, 3) ->shouldIterateAs([]); } @@ -1685,17 +1696,6 @@ static function ($letter) { ->shouldIterateAs([]); } - public function it_can_skip(): void - { - $this::fromIterable(range('A', 'F')) - ->skip(3) - ->shouldIterateAs([3 => 'D', 4 => 'E', 5 => 'F']); - - $this::fromIterable(range('A', 'F')) - ->skip(3, 3) - ->shouldIterateAs([]); - } - public function it_can_slice(): void { $this::fromIterable(range(0, 10)) diff --git a/src/Collection.php b/src/Collection.php index b461e33a5..90c034281 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -29,6 +29,7 @@ use loophp\collection\Operation\Diff; use loophp\collection\Operation\DiffKeys; use loophp\collection\Operation\Distinct; +use loophp\collection\Operation\Drop; use loophp\collection\Operation\DropWhile; use loophp\collection\Operation\Explode; use loophp\collection\Operation\Falsy; @@ -78,7 +79,6 @@ use loophp\collection\Operation\Scale; use loophp\collection\Operation\Shuffle; use loophp\collection\Operation\Since; -use loophp\collection\Operation\Skip; use loophp\collection\Operation\Slice; use loophp\collection\Operation\Sort; use loophp\collection\Operation\Split; @@ -325,6 +325,11 @@ public function distinct(): CollectionInterface return $this->run(Distinct::of()); } + public function drop(int ...$counts): CollectionInterface + { + return $this->run(Drop::of()(...$counts)); + } + public function dropWhile(callable $callback): CollectionInterface { return $this->run(DropWhile::of()($callback)); @@ -660,11 +665,6 @@ public function since(callable ...$callbacks): CollectionInterface return $this->run(Since::of()(...$callbacks)); } - public function skip(int ...$counts): CollectionInterface - { - return $this->run(Skip::of()(...$counts)); - } - public function slice(int $offset, int $length = -1): CollectionInterface { return $this->run(Slice::of()($offset)($length)); diff --git a/src/Contract/Collection.php b/src/Contract/Collection.php index 3df5bfca2..bb63c73b9 100644 --- a/src/Contract/Collection.php +++ b/src/Contract/Collection.php @@ -23,6 +23,7 @@ use loophp\collection\Contract\Operation\Diffable; use loophp\collection\Contract\Operation\Diffkeysable; use loophp\collection\Contract\Operation\Distinctable; +use loophp\collection\Contract\Operation\Dropable; use loophp\collection\Contract\Operation\DropWhileable; use loophp\collection\Contract\Operation\Explodeable; use loophp\collection\Contract\Operation\Falsyable; @@ -71,7 +72,6 @@ use loophp\collection\Contract\Operation\Scaleable; use loophp\collection\Contract\Operation\Shuffleable; use loophp\collection\Contract\Operation\Sinceable; -use loophp\collection\Contract\Operation\Skipable; use loophp\collection\Contract\Operation\Sliceable; use loophp\collection\Contract\Operation\Sortable; use loophp\collection\Contract\Operation\Splitable; @@ -110,6 +110,7 @@ * @template-extends Diffable * @template-extends Diffkeysable * @template-extends Distinctable + * @template-extends Dropable * @template-extends DropWhileable * @template-extends Explodeable * @template-extends Filterable @@ -153,7 +154,6 @@ * @template-extends Scaleable * @template-extends Shuffleable * @template-extends Sinceable - * @template-extends Skipable * @template-extends Sliceable * @template-extends Sortable * @template-extends Splitable @@ -187,6 +187,7 @@ interface Collection extends Diffable, Diffkeysable, Distinctable, + Dropable, DropWhileable, Explodeable, Falsyable, @@ -237,7 +238,6 @@ interface Collection extends Scaleable, Shuffleable, Sinceable, - Skipable, Sliceable, Sortable, Splitable, diff --git a/src/Contract/Operation/Skipable.php b/src/Contract/Operation/Dropable.php similarity index 84% rename from src/Contract/Operation/Skipable.php rename to src/Contract/Operation/Dropable.php index 566978bb5..251e0dc11 100644 --- a/src/Contract/Operation/Skipable.php +++ b/src/Contract/Operation/Dropable.php @@ -11,7 +11,7 @@ * @psalm-template TKey of array-key * @psalm-template T */ -interface Skipable +interface Dropable { /** * Skip the n items of a collection. @@ -20,5 +20,5 @@ interface Skipable * * @psalm-return \loophp\collection\Contract\Collection */ - public function skip(int ...$counts): Collection; + public function drop(int ...$counts): Collection; } diff --git a/src/Operation/Skip.php b/src/Operation/Drop.php similarity index 95% rename from src/Operation/Skip.php rename to src/Operation/Drop.php index 51a3dd6d3..140393b0f 100644 --- a/src/Operation/Skip.php +++ b/src/Operation/Drop.php @@ -14,7 +14,7 @@ * @psalm-template TKey of array-key * @psalm-template T */ -final class Skip extends AbstractOperation +final class Drop extends AbstractOperation { /** * @psalm-return Closure(int...): Closure(Iterator): Generator diff --git a/src/Operation/Slice.php b/src/Operation/Slice.php index 976c517ea..a4ec52f3b 100644 --- a/src/Operation/Slice.php +++ b/src/Operation/Slice.php @@ -40,7 +40,7 @@ static function (int $length = -1) use ($offset): Closure { */ static function (Iterator $iterator) use ($offset, $length): Generator { /** @psalm-var callable(Iterator): Generator $skip */ - $skip = Skip::of()($offset); + $skip = Drop::of()($offset); if (-1 === $length) { return yield from $skip($iterator); diff --git a/src/Operation/Tail.php b/src/Operation/Tail.php index 266281194..fa5b1db0b 100644 --- a/src/Operation/Tail.php +++ b/src/Operation/Tail.php @@ -27,7 +27,7 @@ public function __invoke(): Closure * @psalm-return Generator */ static function (Iterator $iterator): Generator { - return yield from Skip::of()(1)($iterator); + return yield from Drop::of()(1)($iterator); }; } }