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 19, 2021
1 parent ccfa9c3 commit b142df1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 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;
}
11 changes: 6 additions & 5 deletions src/Operation/Reduction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,29 @@ final class Reduction extends AbstractOperation
* @pure
*
* @template V
* @template W
*
* @return Closure(callable(V, T, TKey, Iterator<TKey, T>): V): Closure (V): Closure(Iterator<TKey, T>): Generator<TKey, V>
* @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(V, T, TKey, Iterator<TKey, T>): V $callback
* @param callable(V|W, T, TKey, Iterator<TKey, T>): W $callback
*
* @return Closure(V): Closure(Iterator<TKey, T>): Generator<TKey, V>
* @return Closure(V): Closure(Iterator<TKey, T>): Generator<TKey, V|W>
*/
static fn (callable $callback): Closure =>
/**
* @param V $initial
*
* @return Closure(Iterator<TKey, T>): Generator<TKey, V>
* @return Closure(Iterator<TKey, T>): Generator<TKey, V|W>
*/
static fn ($initial): Closure =>
/**
* @param Iterator<TKey, T> $iterator
*
* @return Generator<TKey, V>
* @return Generator<TKey, V|W>
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
foreach ($iterator as $key => $value) {
Expand Down

0 comments on commit b142df1

Please sign in to comment.