Skip to content

Commit

Permalink
Proposal: Update Reduction operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 18, 2021
1 parent c535406 commit 9db7b32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/Contract/Operation/Reductionable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace loophp\collection\Contract\Operation;

use Iterator;
use loophp\collection\Contract\Collection;

/**
Expand All @@ -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<TKey, T>
* @param callable(V|W, T, TKey, Iterator<TKey, T>): W $callback
* @param V $initial
*
* @return Collection<TKey, V|W>
*/
public function reduction(callable $callback, $initial = null): Collection;
}
17 changes: 10 additions & 7 deletions src/Operation/Reduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,30 @@ final class Reduction extends AbstractOperation
/**
* @pure
*
* @return Closure(callable((T|null), T, TKey, Iterator<TKey, T>): (T|null)):Closure (T|null): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @template V
* @template W
*
* @return Closure(callable(V|W, T, TKey, Iterator<TKey, T>): W): Closure(V): Closure(Iterator<TKey, T>): Generator<TKey, V|W>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(T|null, T, TKey, Iterator<TKey, T>):(T|null) $callback
* @param callable(V|W, T, TKey, Iterator<TKey, T>): W $callback
*
* @return Closure(T|null): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @return Closure(V): Closure(Iterator<TKey, T>): Generator<TKey, V|W>
*/
static fn (callable $callback): Closure =>
/**
* @param T|null $initial
* @param V $initial
*
* @return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @return Closure(Iterator<TKey, T>): Generator<TKey, V|W>
*/
static fn ($initial = null): Closure =>
static fn ($initial): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Generator<TKey, T|null>
* @return Generator<TKey, V|W>
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
foreach ($iterator as $key => $value) {
Expand Down

0 comments on commit 9db7b32

Please sign in to comment.