From 9c64bab8c2cb0ae3710c378b52945c2e1d38c46f Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 27 Mar 2022 11:50:34 +0200 Subject: [PATCH 1/2] feat: Simplify `Every` operation. --- src/Operation/Every.php | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/Operation/Every.php b/src/Operation/Every.php index 86f01e8f7..c972ca3b0 100644 --- a/src/Operation/Every.php +++ b/src/Operation/Every.php @@ -24,41 +24,33 @@ final class Every extends AbstractOperation { /** - * @return Closure(callable(int=, T=, TKey=, iterable=): bool): Closure(callable(bool, int, T, TKey, iterable...): mixed): Closure(iterable): Generator + * @return Closure(...callable(int=, T=, TKey=, iterable=): bool): Closure(iterable): Generator */ public function __invoke(): Closure { return /** - * @param callable(int=, T=, TKey=, iterable=): bool $predicate + * @param callable(int=, T=, TKey=, iterable=): bool ...$predicates * - * @return Closure(callable(bool, int, T=, TKey=, iterable=): mixed): Closure(iterable): Generator + * @return Closure(iterable): Generator */ static function (callable ...$predicates): Closure { return /** - * @param callable(bool, int, T=, TKey=, iterable=): mixed $return + * @param iterable $iterable * - * @return Closure(iterable): Generator + * @return Generator */ - static function (callable $return) use ($predicates): Closure { - return - /** - * @param iterable $iterable - * - * @return Generator - */ - static function (iterable $iterable) use ($return, $predicates): Generator { - $predicate = CallbacksArrayReducer::or()($predicates); + static function (iterable $iterable) use ($predicates): Generator { + $predicate = CallbacksArrayReducer::or()($predicates); - foreach ((new Pack())()($iterable) as $index => [$key, $value]) { - if (false === $predicate($index, $value, $key, $iterable)) { - return yield $return(false, $index, $value, $key, $iterable); - } - } + foreach ((new Pack())()($iterable) as $index => [$key, $value]) { + if (false === $predicate($index, $value, $key, $iterable)) { + return yield $index => false; + } + } - return yield true; - }; + yield true; }; }; } From 5359b54cb7d10fbd4688e89ec912b6a63134b7a9 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 27 Mar 2022 11:50:56 +0200 Subject: [PATCH 2/2] refactor: Refactor operations depending on `Every`. --- src/Collection.php | 2 +- src/Operation/Column.php | 12 ++++-------- src/Operation/DropWhile.php | 4 ++-- src/Operation/Equals.php | 2 +- src/Operation/MatchOne.php | 2 -- src/Operation/ScanLeft1.php | 2 +- src/Operation/TakeWhile.php | 4 ++-- src/Operation/Until.php | 4 ++-- 8 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index cfe97d007..ac3f0c0f5 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -361,7 +361,7 @@ public function equals(iterable $other): bool public function every(callable ...$callbacks): bool { - return (new Every())()(static fn (int $index, $value, $key, iterable $iterable) => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable))(static fn (bool $r): bool => $r)($this) + return (new Every())()(static fn (int $index, $value, $key, iterable $iterable) => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable))($this) ->current(); } diff --git a/src/Operation/Column.php b/src/Operation/Column.php index a0d50611a..7e8dbe3a0 100644 --- a/src/Operation/Column.php +++ b/src/Operation/Column.php @@ -34,19 +34,15 @@ public function __invoke(): Closure static function ($column): Closure { $filterCallbackBuilder = /** - * @param mixed $column + * @param T $value + * @param TKey $key */ - static fn ($column): Closure => - /** - * @param T $value - * @param TKey $key - */ - static fn ($value, $key): bool => $key === $column; + static fn ($value, $key): bool => $key === $column; /** @var Closure(iterable): Generator $pipe */ $pipe = (new Pipe())()( (new Transpose())(), - (new Filter())()($filterCallbackBuilder($column)), + (new Filter())()($filterCallbackBuilder), (new Head())(), (new Flatten())()(1) ); diff --git a/src/Operation/DropWhile.php b/src/Operation/DropWhile.php index e852b9e01..932dbe8d2 100644 --- a/src/Operation/DropWhile.php +++ b/src/Operation/DropWhile.php @@ -52,9 +52,9 @@ static function (iterable $iterable) use ($callbacks): Generator { * @param iterable $iterable */ static fn (int $index, $value, $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable) - )(static fn (bool $r, int $index): int => $index)($iteratorAggregate); + )($iteratorAggregate); - $offset = (true === $current = $every->current()) ? 0 : $current; + $offset = (true === $every->current()) ? 0 : $every->key(); yield from (new Limit())()(-1)($offset)($iteratorAggregate); }; diff --git a/src/Operation/Equals.php b/src/Operation/Equals.php index 0c697d293..5fbb3a7b1 100644 --- a/src/Operation/Equals.php +++ b/src/Operation/Equals.php @@ -62,7 +62,7 @@ static function (iterable $other): Closure { */ static fn (int $index, $current): bool => (new Contains())()($current)($otherAggregate)->current(); - yield from (new Every())()($containsCallback)(static fn (bool $i): bool => $i)($iteratorAggregate); + yield from (new Every())()($containsCallback)($iteratorAggregate); }; }; } diff --git a/src/Operation/MatchOne.php b/src/Operation/MatchOne.php index a0ce115ae..797314b01 100644 --- a/src/Operation/MatchOne.php +++ b/src/Operation/MatchOne.php @@ -53,8 +53,6 @@ static function (callable ...$callbacks) use ($matchers): Closure { * @param TKey $key */ static fn (int $index, $value, $key, iterable $iterable): bool => $callback($value, $key, $iterable) !== $matcher($value, $key, $iterable) - )( - static fn (bool $i): bool => $i ), (new Head())(), (new Map())()( diff --git a/src/Operation/ScanLeft1.php b/src/Operation/ScanLeft1.php index a8fa1aa51..a81b7150b 100644 --- a/src/Operation/ScanLeft1.php +++ b/src/Operation/ScanLeft1.php @@ -58,7 +58,7 @@ static function (iterable $iterable) use ($callback): Generator { (new Prepend())()($initial) ); - yield from $pipe($iteratorAggregate->getIterator()); + yield from $pipe($iteratorAggregate); }; } } diff --git a/src/Operation/TakeWhile.php b/src/Operation/TakeWhile.php index 25e19d4fb..7d3a336df 100644 --- a/src/Operation/TakeWhile.php +++ b/src/Operation/TakeWhile.php @@ -52,9 +52,9 @@ static function (iterable $iterable) use ($callbacks): Generator { * @param iterable $iterable */ static fn (int $index, $value, $key, iterable $iterable): bool => CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable) - )(static fn (bool $r, int $index): int => $index)($iteratorAggregate); + )($iteratorAggregate); - $size = (true === $current = $every->current()) ? -1 : $current; + $size = (true === $every->current()) ? -1 : $every->key(); yield from (new Limit())()($size)(0)($iteratorAggregate); }; diff --git a/src/Operation/Until.php b/src/Operation/Until.php index 6c9e42b8a..7b7beba25 100644 --- a/src/Operation/Until.php +++ b/src/Operation/Until.php @@ -52,9 +52,9 @@ static function (iterable $iterable) use ($callbacks): Generator { * @param iterable $iterable */ static fn (int $index, $value, $key, iterable $iterable): bool => !CallbacksArrayReducer::or()($callbacks)($value, $key, $iterable) - )(static fn (bool $r, int $index): int => $index)($iteratorAggregate); + )($iteratorAggregate); - $size = (true === $current = $every->current()) ? -1 : $current + 1; + $size = (true === $every->current()) ? -1 : $every->key() + 1; yield from (new Limit())()($size)(0)($iteratorAggregate); };