Skip to content

Commit

Permalink
Update Map operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 26, 2020
1 parent 810bff8 commit 1702597
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/Operation/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

namespace loophp\collection\Operation;

use ArrayIterator;
use Closure;
use Generator;
use Iterator;
use loophp\collection\Contract\Operation;
use loophp\collection\Transformation\FoldLeft;
use loophp\collection\Transformation\Transform;

/**
* @psalm-template TKey
Expand All @@ -21,25 +18,25 @@ final class Map extends AbstractOperation implements Operation
{
public function __construct(callable ...$callbacks)
{
$this->storage['callbacks'] = new ArrayIterator($callbacks);
$this->storage['callbacks'] = $callbacks;
}

public function __invoke(): Closure
{
return
/**
* @psalm-param \Iterator<TKey, T> $iterator
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param list<callable(T, TKey):(T)> $callbacks
*
* @psalm-return \Generator<TKey, T>
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator, ArrayIterator $callbacks): Generator {
static function (Iterator $iterator, array $callbacks): Generator {
foreach ($iterator as $key => $value) {
$callback = static function ($carry, callable $callback) use ($value, $key) {
return $callback($value, $key);
};

yield $key => (new Transform(new FoldLeft($callback, $value)))($callbacks);
yield $key => array_reduce($callbacks, $callback, $value);
}
};
}
Expand Down

0 comments on commit 1702597

Please sign in to comment.