Skip to content

Commit

Permalink
Update Tail operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 22, 2020
1 parent a8cb98f commit 1d15657
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 43 deletions.
8 changes: 4 additions & 4 deletions docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1163,16 +1163,16 @@ Signature: ``Collection::split(callable ...$callbacks);``
tail
~~~~

Get last collection items of a collection.
Get the collection items except the first.

Interface: `Tailable`_

Signature: ``Collection::tail(int $length = 1);``
Signature: ``Collection::tail();``

.. code-block:: php
$collection = Collection::with(['a', 'b', 'c'])
->tail(2);
Collection::with(['a', 'b', 'c'])
->tail(); // [1 => 'b', 2 => 'c']
transpose
~~~~~~~~~
Expand Down
23 changes: 1 addition & 22 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use loophp\collection\Contract\Operation;
use loophp\collection\Contract\Transformation;
use loophp\collection\Operation\AbstractOperation;
use OutOfRangeException;
use PhpSpec\ObjectBehavior;
use stdClass;

Expand Down Expand Up @@ -1785,27 +1784,7 @@ public function it_can_tail(): void
{
$this::fromIterable(range('A', 'F'))
->tail()
->shouldIterateAs([5 => 'F']);

$this::fromIterable(range('A', 'F'))
->tail(3)
->shouldIterateAs([3 => 'D', 4 => 'E', 5 => 'F']);

$this::fromIterable(range('A', 'F'))
->tail(-5)
->shouldThrow(OutOfRangeException::class)
->during('all');

$this::fromIterable(range('A', 'F'))
->tail(100)
->shouldIterateAs(range('A', 'F'));

$this::fromIterable(['a', 'b', 'c', 'd', 'a'])
->flip()
->flip()
->tail(2)
->all()
->shouldIterateAs([3 => 'd', 4 => 'a']);
->shouldIterateAs([1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F']);
}

public function it_can_transpose(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,9 @@ public function split(callable ...$callbacks): CollectionInterface
return $this->run(new Split(...$callbacks));
}

public function tail(?int $length = null): CollectionInterface
public function tail(): CollectionInterface
{
return $this->run(new Tail($length));
return $this->run(new Tail());
}

public static function times(int $number = 0, ?callable $callback = null): CollectionInterface
Expand Down
4 changes: 1 addition & 3 deletions src/Contract/Operation/Tailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
interface Tailable
{
/**
* Get last collection items of a collection.
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function tail(?int $length = null): Collection;
public function tail(): Collection;
}
14 changes: 2 additions & 12 deletions src/Operation/Tail.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
*/
final class Tail extends AbstractOperation implements Operation
{
public function __construct(?int $length = null)
{
$this->storage['length'] = $length ?? 1;
}

public function __invoke(): Closure
{
return
Expand All @@ -30,13 +25,8 @@ public function __invoke(): Closure
*
* @psalm-return \Generator<Tkey, T>
*/
static function (Iterator $iterator, int $length): Generator {
return yield from (
new Run(
new Skip(iterator_count($iterator) - $length),
new Limit($length)
)
)($iterator);
static function (Iterator $iterator): Generator {
return yield from (new Run(new Skip(1)))($iterator);
};
}
}

0 comments on commit 1d15657

Please sign in to comment.