From e1d91ec45955777b0b2e25d26c8dadd6d03013d1 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Wed, 30 Dec 2020 13:51:09 +0100 Subject: [PATCH] refactor: Simplify operations. --- src/Operation/Drop.php | 6 +++--- src/Operation/ScanLeft.php | 27 ++++++++------------------- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/Operation/Drop.php b/src/Operation/Drop.php index aba55efac..56c091dc9 100644 --- a/src/Operation/Drop.php +++ b/src/Operation/Drop.php @@ -32,12 +32,12 @@ public function __invoke(): Closure * * @psalm-return Generator */ - static function (Iterator $iterator) use ($offsets): Generator { - if (!$iterator->valid()) { + static function (Iterator $iterator) use ($offsets): Iterator { + if (false === $iterator->valid()) { return new EmptyIterator(); } - return yield from new LimitIterator($iterator, array_sum($offsets)); + return new LimitIterator($iterator, array_sum($offsets)); }; } } diff --git a/src/Operation/ScanLeft.php b/src/Operation/ScanLeft.php index 3d3d6bf29..c0a88c513 100644 --- a/src/Operation/ScanLeft.php +++ b/src/Operation/ScanLeft.php @@ -35,25 +35,14 @@ public function __invoke(): Closure * * @psalm-return Closure(Iterator): Generator */ - static fn ($initial = null): Closure => - /** - * @psalm-param Iterator $iterator - * - * @psalm-return Generator - */ - static function (Iterator $iterator) use ($callback, $initial): Generator { - // @todo: See if we cannot find a better way to do this. - if (false === $iterator->valid()) { - return yield from []; - } + static function ($initial = null) use ($callback): Closure { + /** @psalm-var Closure(Iterator): Generator $pipe */ + $pipe = Pipe::of()( + Reduction::of()($callback)($initial), + Prepend::of()($initial) + ); - /** @psalm-var Closure(Iterator): Generator $pipe */ - $pipe = Pipe::of()( - Reduction::of()($callback)($initial), - Prepend::of()($initial) - ); - - return yield from $pipe($iterator); - }; + return $pipe; + }; } }