From 9db7b322669d7995c94915f49e1987bf7bb0750f Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Sun, 18 Jul 2021 19:49:17 +0200 Subject: [PATCH] Proposal: Update Reduction operations. --- src/Contract/Operation/Reductionable.php | 12 +++++++++--- src/Operation/Reduction.php | 17 ++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Contract/Operation/Reductionable.php b/src/Contract/Operation/Reductionable.php index f4acf3a9d..c7d2a8779 100644 --- a/src/Contract/Operation/Reductionable.php +++ b/src/Contract/Operation/Reductionable.php @@ -9,6 +9,7 @@ namespace loophp\collection\Contract\Operation; +use Iterator; use loophp\collection\Contract\Collection; /** @@ -18,11 +19,16 @@ interface Reductionable { /** - * Reduce a collection of items through a given callback. + * Reduce a collection of items through a given callback and + * and yield each intermediary results. * - * @param mixed $initial + * @template V + * @template W * - * @return Collection + * @param callable(V|W, T, TKey, Iterator): W $callback + * @param V $initial + * + * @return Collection */ public function reduction(callable $callback, $initial = null): Collection; } diff --git a/src/Operation/Reduction.php b/src/Operation/Reduction.php index 1a9215b44..9e314843e 100644 --- a/src/Operation/Reduction.php +++ b/src/Operation/Reduction.php @@ -26,27 +26,30 @@ final class Reduction extends AbstractOperation /** * @pure * - * @return Closure(callable((T|null), T, TKey, Iterator): (T|null)):Closure (T|null): Closure(Iterator): Generator + * @template V + * @template W + * + * @return Closure(callable(V|W, T, TKey, Iterator): W): Closure(V): Closure(Iterator): Generator */ public function __invoke(): Closure { return /** - * @param callable(T|null, T, TKey, Iterator):(T|null) $callback + * @param callable(V|W, T, TKey, Iterator): W $callback * - * @return Closure(T|null): Closure(Iterator): Generator + * @return Closure(V): Closure(Iterator): Generator */ static fn (callable $callback): Closure => /** - * @param T|null $initial + * @param V $initial * - * @return Closure(Iterator): Generator + * @return Closure(Iterator): Generator */ - static fn ($initial = null): Closure => + static fn ($initial): Closure => /** * @param Iterator $iterator * - * @return Generator + * @return Generator */ static function (Iterator $iterator) use ($callback, $initial): Generator { foreach ($iterator as $key => $value) {