-
-
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
7 changed files
with
159 additions
and
112 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
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use Closure; | ||
use Generator; | ||
use Iterator; | ||
use loophp\collection\Contract\Operation; | ||
use loophp\collection\Transformation\Run; | ||
|
||
/** | ||
* @psalm-template TKey | ||
* @psalm-template TKey of array-key | ||
* @psalm-template T | ||
* | ||
* @implements Operation<TKey, T> | ||
*/ | ||
final class Last extends AbstractOperation implements Operation | ||
{ | ||
/** | ||
* @var callable|Closure | ||
* @psalm-var callable(T, TKey):(bool)|\Closure(T, TKey):(bool) | ||
*/ | ||
private $callback; | ||
|
||
/** | ||
* @psalm-param \Closure(T, TKey):(bool)|callable(T, TKey):(bool)|null $callback | ||
* @psalm-param T|null $default | ||
*/ | ||
public function __construct(?callable $callback = null, int $size = 1) | ||
{ | ||
$defaultCallback = | ||
/** | ||
* @param mixed $value | ||
* @param mixed $key | ||
* @psalm-param T $value | ||
* @psalm-param TKey $key | ||
* @psalm-param Iterator<TKey, T> $iterator | ||
*/ | ||
static function ($value, $key, Iterator $iterator): bool { | ||
return true; | ||
}; | ||
|
||
$this->storage = [ | ||
'callback' => $callback ?? $defaultCallback, | ||
'size' => $size, | ||
]; | ||
} | ||
|
||
public function __invoke(): Closure | ||
{ | ||
return | ||
/** | ||
* @psalm-param \Iterator<TKey, T> $iterator | ||
* | ||
* @psalm-return \Generator<TKey, T> | ||
*/ | ||
static function (Iterator $iterator, callable $callback, int $size): Generator { | ||
$callback = | ||
/** | ||
* @param mixed $value | ||
* @param mixed $key | ||
* @psalm-param T $value | ||
* @psalm-param TKey $key | ||
* @psalm-param Iterator<TKey, T> $iterator | ||
*/ | ||
static function ($value, $key, Iterator $iterator) use ($callback): bool { | ||
return true === $callback($value, $key, $iterator); | ||
}; | ||
|
||
return yield from (new Run(new Filter($callback), new Reverse(), new Limit($size)))($iterator); | ||
}; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.