Skip to content

Commit

Permalink
refactor: Align TakeWhile and Until operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Oct 10, 2020
1 parent aa4df6e commit b680be9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 20 deletions.
56 changes: 50 additions & 6 deletions src/Operation/TakeWhile.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @psalm-template T
*
* phpcs:disable Generic.Files.LineLength.TooLong
* phpcs:disable Generic.WhiteSpace.ScopeIndent.IncorrectExact
*/
final class TakeWhile extends AbstractOperation
{
Expand All @@ -28,25 +29,68 @@ public function __invoke(): Closure
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (callable $callback): Closure {
static function (callable ...$callbacks): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callback): Generator {
static function (Iterator $iterator) use ($callbacks): Generator {
$reducerCallback =
/**
* @psalm-param TKey $key
*
* @psalm-return Closure(T): Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*
* @param mixed $key
*/
static function ($key): Closure {
return
/**
* @psalm-param T $current
*
* @psalm-return Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*
* @param mixed $current
*/
static function ($current) use ($key): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static function (Iterator $iterator) use ($key, $current): Closure {
return
/**
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static function (bool $carry, callable $callable) use ($key, $current, $iterator): bool {
return ($callable($current, $key, $iterator)) ?
$carry :
false;
};
};
};
};

for (; $iterator->valid(); $iterator->next()) {
$key = $iterator->key();
$current = $iterator->current();

if (true === $callback($current, $key, $iterator)) {
yield $key => $current;
$result = array_reduce(
$callbacks,
$reducerCallback($key)($current)($iterator),
true
);

continue;
if (false === $result) {
break;
}

break;
yield $key => $current;
}
};
};
Expand Down
36 changes: 22 additions & 14 deletions src/Operation/Until.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,47 @@ static function (Iterator $iterator) use ($callbacks): Generator {
/**
* @psalm-param TKey $key
*
* @psalm-return Closure(T): Closure(bool, callable(T, TKey): bool): bool
* @psalm-return Closure(T): Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*
* @param mixed $key
*/
static function ($key): Closure {
return
/**
* @psalm-param T $value
* @psalm-param T $current
*
* @psalm-return Closure(bool, callable(T, TKey): bool): bool
* @psalm-return Closure(Iterator<TKey, T>): Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*
* @param mixed $value
* @param mixed $current
*/
static function ($value) use ($key): Closure {
static function ($current) use ($key): Closure {
return
/**
* @psalm-param bool $carry
* @psalm-param callable(T, TKey): bool $callable
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static function (bool $carry, callable $callable) use ($key, $value): bool {
return ($callable($value, $key)) ?
$carry :
false;
static function (Iterator $iterator) use ($key, $current): Closure {
return
/**
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static function (bool $carry, callable $callable) use ($key, $current, $iterator): bool {
return ($callable($current, $key, $iterator)) ?
$carry :
false;
};
};
};
};

foreach ($iterator as $key => $value) {
yield $key => $value;
foreach ($iterator as $key => $current) {
yield $key => $current;

$result = array_reduce(
$callbacks,
$reducerCallback($key)($value),
$reducerCallback($key)($current)($iterator),
true
);

Expand Down

0 comments on commit b680be9

Please sign in to comment.