-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Contract; | ||
|
||
/** | ||
* Interface Reverseable. | ||
*/ | ||
interface Reverseable | ||
{ | ||
/** | ||
* TODO. | ||
* | ||
* @return \drupol\collection\Contract\Collection<mixed> | ||
*/ | ||
public function reverse(): Base; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Contract; | ||
|
||
/** | ||
* Interface Tailable. | ||
*/ | ||
interface Tailable | ||
{ | ||
/** | ||
* @param int $length | ||
* | ||
* @return \drupol\collection\Contract\Collection<mixed> | ||
*/ | ||
public function tail(int $length): Base; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Operation; | ||
|
||
use Closure; | ||
use drupol\collection\Contract\Operation; | ||
use drupol\collection\Transformation\All; | ||
use Generator; | ||
|
||
/** | ||
* Class Reverse. | ||
*/ | ||
final class Reverse implements Operation | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function on(iterable $collection): Closure | ||
{ | ||
return static function () use ($collection): Generator { | ||
$all = (new All())->on($collection); | ||
|
||
for (end($all); null !== ($key = key($all)); prev($all)) { | ||
yield $key => current($all); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace drupol\collection\Operation; | ||
|
||
use Closure; | ||
use drupol\collection\Contract\Operation; | ||
use drupol\collection\Transformation\Count; | ||
use Generator; | ||
|
||
/** | ||
* Class Tail. | ||
*/ | ||
final class Tail implements Operation | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $length; | ||
|
||
/** | ||
* Tail constructor. | ||
* | ||
* @param int $length | ||
*/ | ||
public function __construct(int $length) | ||
{ | ||
$this->length = $length; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function on(iterable $collection): Closure | ||
{ | ||
$offset = (new Count())->on($collection) - $this->length; | ||
$length = $this->length; | ||
|
||
return static function () use ($offset, $length, $collection): Generator { | ||
yield from (new Limit($length))->on((new Skip($offset))->on($collection)())(); | ||
}; | ||
} | ||
} |