Skip to content

Commit

Permalink
refactor: Return instead of yield.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 30, 2020
1 parent 9472d03 commit a7b6a9c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
11 changes: 5 additions & 6 deletions src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use CallbackFilterIterator;
use Closure;
use Generator;
use Iterator;

/**
Expand All @@ -19,23 +18,23 @@
final class Filter extends AbstractOperation
{
/**
* @psalm-return Closure(callable(T , TKey , Iterator<TKey, T> ): bool ...):Closure (Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(callable(T , TKey , Iterator<TKey, T> ): bool ...): Closure (Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool ...$callbacks
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
static fn (callable ...$callbacks): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static function (Iterator $iterator) use ($callbacks): Generator {
static function (Iterator $iterator) use ($callbacks): Iterator {
$defaultCallback =
/**
* @param mixed $value
Expand All @@ -52,7 +51,7 @@ static function (Iterator $iterator) use ($callbacks): Generator {
[$defaultCallback] :
$callbacks;

return yield from array_reduce(
return array_reduce(
$callbacks,
static fn (Iterator $carry, callable $callback): CallbackFilterIterator => new CallbackFilterIterator($carry, $callback),
$iterator
Expand Down
19 changes: 13 additions & 6 deletions src/Operation/Head.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Closure;
use EmptyIterator;
use Generator;
use Iterator;

/**
Expand All @@ -17,22 +16,30 @@
final class Head extends AbstractOperation
{
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static function (Iterator $iterator): Generator {
if (!$iterator->valid()) {
static function (Iterator $iterator): Iterator {
$isEmpty = true;

foreach ($iterator as $key => $current) {
$isEmpty = false;

break;
}

if (true === $isEmpty) {
return new EmptyIterator();
}

return yield $iterator->key() => $iterator->current();
return yield $key => $current;
};
}
}
11 changes: 5 additions & 6 deletions src/Operation/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace loophp\collection\Operation;

use Closure;
use Generator;
use Iterator;
use LimitIterator;

Expand All @@ -19,24 +18,24 @@
final class Limit extends AbstractOperation
{
/**
* @psalm-return Closure(int): Closure(int): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(int): Closure(int): Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
static fn (int $count = -1): Closure =>
/**
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
static fn (int $offset = 0): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static fn (Iterator $iterator): Generator => yield from new LimitIterator($iterator, $offset, $count);
static fn (Iterator $iterator): Iterator => new LimitIterator($iterator, $offset, $count);
}
}
14 changes: 7 additions & 7 deletions src/Operation/Pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,33 @@
final class Pipe extends AbstractOperation
{
/**
* @psalm-return Closure(callable(Iterator<TKey, T> ): Generator<TKey, T> ...):Closure (Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(callable(Iterator<TKey, T> ): Iterator<TKey, T> ...): Closure (Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param callable(Iterator<TKey, T>): Generator<TKey, T> ...$operations
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
static fn (callable ...$operations): Closure =>
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static function (Iterator $iterator) use ($operations): Generator {
static function (Iterator $iterator) use ($operations): Iterator {
$callback =
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param callable(Iterator<TKey, T>): Generator<TKey, T> $fn
* @psalm-param callable(Iterator<TKey, T>): Iterator<TKey, T> $fn
*
* @psalm-return Generator<TKey, T>
* @psalm-return Iterator<TKey, T>
*/
static fn (Iterator $iterator, callable $fn): Iterator => $fn($iterator);

return yield from array_reduce($operations, $callback, $iterator);
return array_reduce($operations, $callback, $iterator);
};
}
}
4 changes: 2 additions & 2 deletions src/Operation/ScanLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __invoke(): Closure
*
* @psalm-return Generator<int|TKey, T|null>
*/
static function (Iterator $iterator) use ($callback): Generator {
static function (Iterator $iterator) use ($callback): Iterator {
$initial = $iterator->current();

/** @psalm-var Closure(Iterator<TKey, T>):(Generator<int|TKey, T|null>) $pipe */
Expand All @@ -44,7 +44,7 @@ static function (Iterator $iterator) use ($callback): Generator {
Prepend::of()($initial)
);

return yield from $pipe($iterator);
return $pipe($iterator);
};
}
}

0 comments on commit a7b6a9c

Please sign in to comment.