Skip to content

Commit

Permalink
refactor: Update Pipe operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jul 18, 2021
1 parent 2f25606 commit c535406
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/Operation/Pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace loophp\collection\Operation;

use Closure;
use Iterator;

/**
* @immutable
Expand All @@ -24,33 +25,35 @@ final class Pipe extends AbstractOperation
/**
* @pure
*
* @return Closure(callable(iterable<TKey, T>): iterable<TKey, T> ...): Closure(iterable<TKey, T>): iterable<TKey, T>
* @return Closure(callable(Iterator<TKey, T>): Iterator<TKey, T> ...): Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param callable(iterable<TKey, T>): iterable<TKey, T> ...$operations
* @param callable(Iterator<TKey, T>): Iterator<TKey, T> ...$operations
*
* @return Closure(iterable<TKey, T>): iterable<TKey, T>
* @return Closure(Iterator<TKey, T>): Iterator<TKey, T>
*/
static fn (callable ...$operations): Closure =>
/**
* @param iterable<TKey, T> $iterator
* @param Iterator<TKey, T> $iterator
*
* @return iterable<TKey, T>
* @return Iterator<TKey, T>
*/
static function (iterable $iterator) use ($operations): iterable {
$callback =
/**
* @param iterable<TKey, T> $iterator
* @param callable(iterable<TKey, T>): iterable<TKey, T> $callable
*
* @return iterable<TKey, T>
*/
static fn (iterable $iterator, callable $callable): iterable => $callable($iterator);

return array_reduce($operations, $callback, $iterator);
};
static fn (Iterator $iterator): Iterator => array_reduce(
$operations,
/**
* TODO: Should we return a new ClosureIterator here ?
* Something like: "new ClosureIterator($callable, $iterator)".
*
* @param Iterator<TKey, T> $iterator
* @param callable(Iterator<TKey, T>): Iterator<TKey, T> $callable
*
* @return Iterator<TKey, T>
*/
static fn (Iterator $iterator, callable $callable): Iterator => $callable($iterator),
$iterator
);
}
}

0 comments on commit c535406

Please sign in to comment.