Skip to content

Commit

Permalink
Rename Skip into Drop.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Sep 7, 2020
1 parent cf816a3 commit 0c6b01a
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
30 changes: 15 additions & 15 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~

Expand Down Expand Up @@ -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
~~~~~

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

Expand Down Expand Up @@ -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))
Expand Down
12 changes: 6 additions & 6 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions src/Contract/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -110,6 +110,7 @@
* @template-extends Diffable<TKey, T>
* @template-extends Diffkeysable<TKey, T>
* @template-extends Distinctable<TKey, T>
* @template-extends Dropable<TKey, T>
* @template-extends DropWhileable<TKey, T>
* @template-extends Explodeable<TKey, T>
* @template-extends Filterable<TKey, T>
Expand Down Expand Up @@ -153,7 +154,6 @@
* @template-extends Scaleable<TKey, T>
* @template-extends Shuffleable<TKey, T>
* @template-extends Sinceable<TKey, T>
* @template-extends Skipable<TKey, T>
* @template-extends Sliceable<TKey, T>
* @template-extends Sortable<TKey, T>
* @template-extends Splitable<TKey, T>
Expand Down Expand Up @@ -187,6 +187,7 @@ interface Collection extends
Diffable,
Diffkeysable,
Distinctable,
Dropable,
DropWhileable,
Explodeable,
Falsyable,
Expand Down Expand Up @@ -237,7 +238,6 @@ interface Collection extends
Scaleable,
Shuffleable,
Sinceable,
Skipable,
Sliceable,
Sortable,
Splitable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @psalm-template TKey of array-key
* @psalm-template T
*/
interface Skipable
interface Dropable
{
/**
* Skip the n items of a collection.
Expand All @@ -20,5 +20,5 @@ interface Skipable
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function skip(int ...$counts): Collection;
public function drop(int ...$counts): Collection;
}
2 changes: 1 addition & 1 deletion src/Operation/Skip.php → src/Operation/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<TKey, T>): Generator<TKey, T>
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static function (int $length = -1) use ($offset): Closure {
*/
static function (Iterator $iterator) use ($offset, $length): Generator {
/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $skip */
$skip = Skip::of()($offset);
$skip = Drop::of()($offset);

if (-1 === $length) {
return yield from $skip($iterator);
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Tail.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __invoke(): Closure
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator): Generator {
return yield from Skip::of()(1)($iterator);
return yield from Drop::of()(1)($iterator);
};
}
}

0 comments on commit 0c6b01a

Please sign in to comment.