-
-
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
6 changed files
with
105 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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract\Operation; | ||
|
||
use loophp\collection\Contract\Collection; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
*/ | ||
interface Tailsable | ||
{ | ||
/** | ||
* @psalm-return \loophp\collection\Contract\Collection<TKey, T> | ||
*/ | ||
public function tails(): Collection; | ||
} |
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use ArrayIterator; | ||
use Closure; | ||
use Generator; | ||
use Iterator; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
*/ | ||
final class Tails extends AbstractOperation | ||
{ | ||
/** | ||
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, array<TKey, T>, mixed, void> | ||
*/ | ||
public function __invoke(): Closure | ||
{ | ||
return | ||
/** | ||
* @psalm-param Iterator<TKey, T> $iterator | ||
* | ||
* @psalm-return Generator<int, array<TKey, T>, mixed, void> | ||
*/ | ||
static function (Iterator $iterator): Generator { | ||
/** @psalm-var Iterator<int, array{0: TKey, 1: T}> */ | ||
$iterator = Pack::of()($iterator); | ||
$data = iterator_to_array($iterator); | ||
|
||
while ([] !== $data) { | ||
/** @psalm-var Iterator<TKey, T> $unpack */ | ||
$unpack = Unpack::of()(new ArrayIterator($data)); | ||
|
||
yield iterator_to_array($unpack); | ||
|
||
array_shift($data); | ||
} | ||
|
||
yield []; | ||
}; | ||
} | ||
} |