From 02c61a141a553af9767656acd7fa863ac2710ec5 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Wed, 18 Aug 2021 22:05:51 +0200 Subject: [PATCH] refactor: Update `Collapse` operation in point free style. (#182) --- src/Operation/Collapse.php | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Operation/Collapse.php b/src/Operation/Collapse.php index b77855262..f57b6f0ad 100644 --- a/src/Operation/Collapse.php +++ b/src/Operation/Collapse.php @@ -28,24 +28,19 @@ final class Collapse extends AbstractOperation */ public function __invoke(): Closure { - return + $filterCallback = /** - * @param Iterator|T> $iterator - * - * @return Generator + * @param T $value */ - static function (Iterator $iterator): Generator { - /** @var Closure(Iterator>): Generator> $filter */ - $filter = Filter::of()( - /** - * @param T $value - */ - static fn ($value): bool => is_iterable($value) - ); + static fn ($value): bool => is_iterable($value); - foreach ($filter($iterator) as $value) { - yield from $value; - } - }; + /** @var Closure(Iterator)>): Generator $pipe */ + $pipe = Pipe::of()( + Filter::of()($filterCallback), + Flatten::of()(1), + ); + + // Point free style. + return $pipe; } }