Skip to content

Commit

Permalink
refactor: Update Split operation.
Browse files Browse the repository at this point in the history
Use the same OR array_reduce callback everywhere.
  • Loading branch information
drupol committed Jan 3, 2021
1 parent 0420906 commit 5b9d153
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/Operation/Split.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,39 @@ public function __invoke(): Closure
static function (Iterator $iterator) use ($type, $callbacks): Generator {
$carry = [];

$reducer =
$reducerCallback =
/**
* @param mixed $key
* @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
*/
static fn ($key): Closure =>
/**
* @param mixed $value
* @psalm-param T $value
* @param mixed $current
* @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
*/
static fn ($value): Closure =>
static fn ($current): Closure =>
/**
* @psalm-param callable(T, TKey): bool $callback
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Closure(bool, callable(T, TKey, Iterator<TKey, T>): bool): bool
*/
static fn (bool $carry, callable $callback): bool => $callback($value, $key) || $carry;
static fn (Iterator $iterator): Closure =>
/**
* @psalm-param bool $carry
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callable
*/
static fn (bool $carry, callable $callable): bool => ($callable($current, $key, $iterator)) || $carry;

foreach ($iterator as $key => $value) {
$callbackReturn = array_reduce($callbacks, $reducer($key)($value), false);
$callbackReturn = array_reduce(
$callbacks,
$reducerCallback($key)($value)($iterator),
false
);

if (Splitable::AFTER === $type) {
$carry[] = $value;
Expand Down

0 comments on commit 5b9d153

Please sign in to comment.