From 841a7c50f3c6c0dbbd1ab2dca5c830cfec2e8c45 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 8 Sep 2020 18:30:38 +0200 Subject: [PATCH] Use Filter operation when it's possible. --- src/Operation/Diff.php | 24 +++++++++++++++++++----- src/Operation/DiffKeys.php | 24 +++++++++++++++++++----- src/Operation/Forget.php | 24 +++++++++++++++++++----- src/Operation/Intersect.php | 24 ++++++++++++++++++------ src/Operation/IntersectKeys.php | 28 ++++++++++++++++++++-------- 5 files changed, 95 insertions(+), 29 deletions(-) diff --git a/src/Operation/Diff.php b/src/Operation/Diff.php index 1f1786f80..e39001225 100644 --- a/src/Operation/Diff.php +++ b/src/Operation/Diff.php @@ -34,11 +34,25 @@ static function (...$values): Closure { * @psalm-return Generator */ static function (Iterator $iterator) use ($values): Generator { - foreach ($iterator as $key => $value) { - if (false === in_array($value, $values, true)) { - yield $key => $value; - } - } + $filterCallbackFactory = static function (array $values): Closure { + return + /** + * @psalm-param T $value + * @psalm-param TKey $key + * @psalm-param Iterator $iterator + * + * @param mixed $value + * @param mixed $key + */ + static function ($value, $key, Iterator $iterator) use ($values): bool { + return false === in_array($value, $values, true); + }; + }; + + /** @psalm-var callable(Iterator): Generator $filter */ + $filter = Filter::of()($filterCallbackFactory($values)); + + return $filter($iterator); }; }; } diff --git a/src/Operation/DiffKeys.php b/src/Operation/DiffKeys.php index d86fa275d..7b5f75bed 100644 --- a/src/Operation/DiffKeys.php +++ b/src/Operation/DiffKeys.php @@ -32,11 +32,25 @@ static function (...$values): Closure { * @psalm-param Iterator $iterator */ static function (Iterator $iterator) use ($values): Generator { - foreach ($iterator as $key => $value) { - if (false === in_array($key, $values, true)) { - yield $key => $value; - } - } + $filterCallbackFactory = static function (array $values): Closure { + return + /** + * @psalm-param T $value + * @psalm-param TKey $key + * @psalm-param Iterator $iterator + * + * @param mixed $value + * @param mixed $key + */ + static function ($value, $key, Iterator $iterator) use ($values): bool { + return false === in_array($key, $values, true); + }; + }; + + /** @psalm-var callable(Iterator): Generator $filter */ + $filter = Filter::of()($filterCallbackFactory($values)); + + return $filter($iterator); }; }; } diff --git a/src/Operation/Forget.php b/src/Operation/Forget.php index 929219bd1..f444d9344 100644 --- a/src/Operation/Forget.php +++ b/src/Operation/Forget.php @@ -34,11 +34,25 @@ static function (...$keys): Closure { * @psalm-return Generator */ static function (Iterator $iterator) use ($keys): Generator { - foreach ($iterator as $key => $value) { - if (false === in_array($key, $keys, true)) { - yield $key => $value; - } - } + $filterCallbackFactory = static function (array $keys): Closure { + return + /** + * @psalm-param T $value + * @psalm-param TKey $key + * @psalm-param Iterator $iterator + * + * @param mixed $value + * @param mixed $key + */ + static function ($value, $key, Iterator $iterator) use ($keys): bool { + return false === in_array($key, $keys, true); + }; + }; + + /** @psalm-var callable(Iterator): Generator $filter */ + $filter = Filter::of()($filterCallbackFactory($keys)); + + return $filter($iterator); }; }; } diff --git a/src/Operation/Intersect.php b/src/Operation/Intersect.php index c6bba2bbb..6ac4a3a47 100644 --- a/src/Operation/Intersect.php +++ b/src/Operation/Intersect.php @@ -34,13 +34,25 @@ static function (...$values): Closure { * @psalm-return Generator */ static function (Iterator $iterator) use ($values): Generator { - foreach ($iterator as $key => $value) { - if (false === in_array($value, $values, true)) { - continue; - } + $filterCallbackFactory = static function (array $values): Closure { + return + /** + * @psalm-param T $value + * @psalm-param TKey $key + * @psalm-param Iterator $iterator + * + * @param mixed $value + * @param mixed $key + */ + static function ($value, $key, Iterator $iterator) use ($values): bool { + return in_array($value, $values, true); + }; + }; - yield $key => $value; - } + /** @psalm-var callable(Iterator): Generator $filter */ + $filter = Filter::of()($filterCallbackFactory($values)); + + return $filter($iterator); }; }; } diff --git a/src/Operation/IntersectKeys.php b/src/Operation/IntersectKeys.php index 7399a1490..afd3286b1 100644 --- a/src/Operation/IntersectKeys.php +++ b/src/Operation/IntersectKeys.php @@ -26,21 +26,33 @@ public function __invoke(): Closure /** * @psalm-param TKey ...$values */ - static function (...$values): Closure { + static function (...$keys): Closure { return /** * @psalm-param Iterator $iterator * * @psalm-return Generator */ - static function (Iterator $iterator) use ($values): Generator { - foreach ($iterator as $key => $value) { - if (false === in_array($key, $values, true)) { - continue; - } + static function (Iterator $iterator) use ($keys): Generator { + $filterCallbackFactory = static function (array $keys): Closure { + return + /** + * @psalm-param T $value + * @psalm-param TKey $key + * @psalm-param Iterator $iterator + * + * @param mixed $value + * @param mixed $key + */ + static function ($value, $key, Iterator $iterator) use ($keys): bool { + return in_array($key, $keys, true); + }; + }; - yield $key => $value; - } + /** @psalm-var callable(Iterator): Generator $filter */ + $filter = Filter::of()($filterCallbackFactory($keys)); + + return $filter($iterator); }; }; }