From 8cfd4ebfff5dc6c7674e5bc3501b862783199cac Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 22 Aug 2019 23:04:51 +0200 Subject: [PATCH] Do not let Operations wrap the return of ::run() in a collection. --- src/Operation/Append.php | 18 ++++++++---------- src/Operation/Chunk.php | 26 +++++++++++++------------- src/Operation/Collapse.php | 16 +++++++--------- src/Operation/Combine.php | 22 ++++++++++------------ src/Operation/Filter.php | 16 +++++++--------- src/Operation/Flatten.php | 30 +++++++++++++++--------------- src/Operation/Flip.php | 12 +++++------- src/Operation/Forget.php | 16 +++++++--------- src/Operation/Intersperse.php | 18 ++++++++---------- src/Operation/Keys.php | 12 +++++------- src/Operation/Last.php | 16 +++++++++------- src/Operation/Limit.php | 18 ++++++++---------- src/Operation/Merge.php | 20 +++++++++----------- src/Operation/Normalize.php | 12 +++++------- src/Operation/Nth.php | 20 +++++++++----------- src/Operation/Only.php | 22 ++++++++++------------ src/Operation/Pad.php | 22 ++++++++++------------ src/Operation/Pluck.php | 14 ++++++-------- src/Operation/Prepend.php | 18 ++++++++---------- src/Operation/Proxy.php | 22 +++++++--------------- src/Operation/Range.php | 12 +++++------- src/Operation/Rebase.php | 6 ++++-- src/Operation/Run.php | 10 ++++++++-- src/Operation/Skip.php | 28 +++++++++++++--------------- src/Operation/Slice.php | 16 +++++++--------- src/Operation/Sort.php | 15 ++++++--------- src/Operation/Walk.php | 18 ++++++++---------- src/Operation/Zip.php | 34 +++++++++++++++++++--------------- 28 files changed, 236 insertions(+), 273 deletions(-) diff --git a/src/Operation/Append.php b/src/Operation/Append.php index 1f672bc9d..3689b2d76 100644 --- a/src/Operation/Append.php +++ b/src/Operation/Append.php @@ -14,20 +14,18 @@ final class Append extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$items] = $this->parameters; - return $collection::with( - static function () use ($items, $collection): \Generator { - foreach ($collection as $item) { - yield $item; - } + return static function () use ($items, $collection): \Generator { + foreach ($collection as $item) { + yield $item; + } - foreach ($items as $item) { - yield $item; - } + foreach ($items as $item) { + yield $item; } - ); + }; } } diff --git a/src/Operation/Chunk.php b/src/Operation/Chunk.php index 798789cc0..237c96e60 100644 --- a/src/Operation/Chunk.php +++ b/src/Operation/Chunk.php @@ -24,28 +24,28 @@ public function __construct(int $size) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$size] = $this->parameters; if (0 >= $size) { - return $collection::with(); + return static function () { + yield from []; + }; } - return $collection::with( - static function () use ($size, $collection): \Generator { - $iterator = $collection->getIterator(); + return static function () use ($size, $collection): \Generator { + $iterator = $collection->getIterator(); - while ($iterator->valid()) { - $values = []; + while ($iterator->valid()) { + $values = []; - for ($i = 0; $iterator->valid() && $i < $size; $i++, $iterator->next()) { - $values[$iterator->key()] = $iterator->current(); - } - - yield $collection::with($values); + for ($i = 0; $iterator->valid() && $i < $size; $i++, $iterator->next()) { + $values[$iterator->key()] = $iterator->current(); } + + yield $collection::with($values); } - ); + }; } } diff --git a/src/Operation/Collapse.php b/src/Operation/Collapse.php index 86241def3..913fbc44c 100644 --- a/src/Operation/Collapse.php +++ b/src/Operation/Collapse.php @@ -15,18 +15,16 @@ final class Collapse extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { - return $collection::with( - static function () use ($collection): \Generator { - foreach ($collection as $item) { - if (\is_array($item) || $item instanceof Collection) { - foreach ($item as $value) { - yield $value; - } + return static function () use ($collection): \Generator { + foreach ($collection as $item) { + if (\is_array($item) || $item instanceof Collection) { + foreach ($item as $value) { + yield $value; } } } - ); + }; } } diff --git a/src/Operation/Combine.php b/src/Operation/Combine.php index 4553a641c..0cf3428bc 100644 --- a/src/Operation/Combine.php +++ b/src/Operation/Combine.php @@ -14,26 +14,24 @@ final class Combine extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$keys] = $this->parameters; - return $collection::with( - static function () use ($keys, $collection): \Generator { - $original = $collection->getIterator(); - $keysIterator = $collection::with($keys)->getIterator(); + return static function () use ($keys, $collection): \Generator { + $original = $collection->getIterator(); + $keysIterator = $collection::with($keys)->getIterator(); - for (; true === ($original->valid() && $keysIterator->valid()); $original->next(), $keysIterator->next() + for (; true === ($original->valid() && $keysIterator->valid()); $original->next(), $keysIterator->next() ) { - yield $keysIterator->current() => $original->current(); - } + yield $keysIterator->current() => $original->current(); + } - if (($original->valid() && !$keysIterator->valid()) || + if (($original->valid() && !$keysIterator->valid()) || (!$original->valid() && $keysIterator->valid()) ) { - \trigger_error('Both keys and values must have the same amount of items.', \E_USER_WARNING); - } + \trigger_error('Both keys and values must have the same amount of items.', \E_USER_WARNING); } - ); + }; } } diff --git a/src/Operation/Filter.php b/src/Operation/Filter.php index 2b85b4018..766fe9653 100644 --- a/src/Operation/Filter.php +++ b/src/Operation/Filter.php @@ -14,7 +14,7 @@ final class Filter extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$callbacks] = $this->parameters; @@ -24,16 +24,14 @@ public function run(BaseCollectionInterface $collection): BaseCollectionInterfac }; } - return $collection::with( - static function () use ($callbacks, $collection): \Generator { - foreach ($callbacks as $callback) { - foreach ($collection as $key => $value) { - if (true === (bool) $callback($value, $key)) { - yield $key => $value; - } + return static function () use ($callbacks, $collection): \Generator { + foreach ($callbacks as $callback) { + foreach ($collection as $key => $value) { + if (true === (bool) $callback($value, $key)) { + yield $key => $value; } } } - ); + }; } } diff --git a/src/Operation/Flatten.php b/src/Operation/Flatten.php index c5487f573..ce56f1a67 100644 --- a/src/Operation/Flatten.php +++ b/src/Operation/Flatten.php @@ -24,26 +24,26 @@ public function __construct(int $depth) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$depth] = $this->parameters; - return $collection::with( - static function () use ($depth, $collection): \Generator { - foreach ($collection as $item) { - if (!\is_array($item) && !$item instanceof BaseCollectionInterface) { - yield $item; - } elseif (1 === $depth) { - foreach ($item as $i) { - yield $i; - } - } else { - foreach ((new Flatten($depth - 1))->run($collection::with($item)) as $flattenItem) { - yield $flattenItem; - } + return static function () use ($depth, $collection): \Generator { + foreach ($collection as $item) { + if (!\is_array($item) && !$item instanceof BaseCollectionInterface) { + yield $item; + } elseif (1 === $depth) { + foreach ($item as $i) { + yield $i; + } + } else { + $flatten = new Flatten($depth - 1); + + foreach ($collection::with($flatten->run($collection::with($item))) as $flattenItem) { + yield $flattenItem; } } } - ); + }; } } diff --git a/src/Operation/Flip.php b/src/Operation/Flip.php index b207b0009..a463d6795 100644 --- a/src/Operation/Flip.php +++ b/src/Operation/Flip.php @@ -14,14 +14,12 @@ final class Flip extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { - return $collection::with( - static function () use ($collection): \Generator { - foreach ($collection as $key => $value) { - yield $value => $key; - } + return static function () use ($collection): \Generator { + foreach ($collection as $key => $value) { + yield $value => $key; } - ); + }; } } diff --git a/src/Operation/Forget.php b/src/Operation/Forget.php index 9c24be724..0b189ad7b 100644 --- a/src/Operation/Forget.php +++ b/src/Operation/Forget.php @@ -14,20 +14,18 @@ final class Forget extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$keys] = $this->parameters; - return $collection::with( - static function () use ($keys, $collection): \Generator { - $keys = \array_flip($keys); + return static function () use ($keys, $collection): \Generator { + $keys = \array_flip($keys); - foreach ($collection as $key => $value) { - if (!\array_key_exists($key, $keys)) { - yield $key => $value; - } + foreach ($collection as $key => $value) { + if (!\array_key_exists($key, $keys)) { + yield $key => $value; } } - ); + }; } } diff --git a/src/Operation/Intersperse.php b/src/Operation/Intersperse.php index ef72bdf5a..44ab38116 100644 --- a/src/Operation/Intersperse.php +++ b/src/Operation/Intersperse.php @@ -30,7 +30,7 @@ public function __construct($elementToIntersperse, int $atEvery = 1, int $startA /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$element, $every, $startAt] = $this->parameters; @@ -42,16 +42,14 @@ public function run(BaseCollectionInterface $collection): BaseCollectionInterfac throw new \InvalidArgumentException('The third parameter must be a positive integer.'); } - return $collection::with( - static function () use ($element, $every, $startAt, $collection): \Generator { - foreach ($collection as $key => $value) { - if (0 === $startAt++ % $every) { - yield $element; - } - - yield $value; + return static function () use ($element, $every, $startAt, $collection): \Generator { + foreach ($collection as $key => $value) { + if (0 === $startAt++ % $every) { + yield $element; } + + yield $value; } - ); + }; } } diff --git a/src/Operation/Keys.php b/src/Operation/Keys.php index e1847d99b..20ff6099e 100644 --- a/src/Operation/Keys.php +++ b/src/Operation/Keys.php @@ -14,14 +14,12 @@ final class Keys extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { - return $collection::with( - static function () use ($collection): \Generator { - foreach ($collection as $key => $value) { - yield $key; - } + return static function () use ($collection): \Generator { + foreach ($collection as $key => $value) { + yield $key; } - ); + }; } } diff --git a/src/Operation/Last.php b/src/Operation/Last.php index 73fe85f91..6f552658e 100644 --- a/src/Operation/Last.php +++ b/src/Operation/Last.php @@ -18,12 +18,14 @@ final class Last extends Operation */ public function run(BaseCollectionInterface $collection) { - return ( - new Reduce( - static function ($carry, $item) { - return $item; - }, - $collection->getIterator()->current() - ))->run($collection); + $reduced = + new Reduce( + static function ($carry, $item) { + return $item; + }, + $collection->getIterator()->current() + ); + + return $reduced->run($collection); } } diff --git a/src/Operation/Limit.php b/src/Operation/Limit.php index 426dcd4bf..904aaac2b 100644 --- a/src/Operation/Limit.php +++ b/src/Operation/Limit.php @@ -24,22 +24,20 @@ public function __construct(int $limit) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$limit] = $this->parameters; - return $collection::with( - static function () use ($limit, $collection): \Generator { - $i = 0; + return static function () use ($limit, $collection): \Generator { + $i = 0; - foreach ($collection as $key => $value) { - yield $key => $value; + foreach ($collection as $key => $value) { + yield $key => $value; - if (++$i === $limit) { - break; - } + if (++$i === $limit) { + break; } } - ); + }; } } diff --git a/src/Operation/Merge.php b/src/Operation/Merge.php index 1f1017662..6bacb29e0 100644 --- a/src/Operation/Merge.php +++ b/src/Operation/Merge.php @@ -14,22 +14,20 @@ final class Merge extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$sources] = $this->parameters; - return $collection::with( - static function () use ($sources, $collection): \Generator { - foreach ($collection as $item) { - yield $item; - } + return static function () use ($sources, $collection): \Generator { + foreach ($collection as $item) { + yield $item; + } - foreach ($sources as $source) { - foreach ($source as $item) { - yield $item; - } + foreach ($sources as $source) { + foreach ($source as $item) { + yield $item; } } - ); + }; } } diff --git a/src/Operation/Normalize.php b/src/Operation/Normalize.php index 6c48d8c88..7134ae5db 100644 --- a/src/Operation/Normalize.php +++ b/src/Operation/Normalize.php @@ -14,14 +14,12 @@ final class Normalize extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { - return $collection::with( - static function () use ($collection) { - foreach ($collection as $item) { - yield $item; - } + return static function () use ($collection) { + foreach ($collection as $item) { + yield $item; } - ); + }; } } diff --git a/src/Operation/Nth.php b/src/Operation/Nth.php index d547b2eb6..3ba4872cb 100644 --- a/src/Operation/Nth.php +++ b/src/Operation/Nth.php @@ -25,22 +25,20 @@ public function __construct(int $step, int $offset) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$step, $offset] = $this->parameters; - return $collection::with( - static function () use ($step, $offset, $collection): \Generator { - $position = 0; + return static function () use ($step, $offset, $collection): \Generator { + $position = 0; - foreach ($collection as $key => $item) { - if ($position++ % $step !== $offset) { - continue; - } - - yield $key => $item; + foreach ($collection as $key => $item) { + if ($position++ % $step !== $offset) { + continue; } + + yield $key => $item; } - ); + }; } } diff --git a/src/Operation/Only.php b/src/Operation/Only.php index ad206b394..46b15a0a3 100644 --- a/src/Operation/Only.php +++ b/src/Operation/Only.php @@ -14,24 +14,22 @@ final class Only extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection) { [$keys] = $this->parameters; - return $collection::with( - static function () use ($keys, $collection): \Generator { - if ([] === $keys) { - yield from $collection; - } else { - $keys = \array_flip($keys); + return static function () use ($keys, $collection): \Generator { + if ([] === $keys) { + yield from $collection; + } else { + $keys = \array_flip($keys); - foreach ($collection as $key => $value) { - if (\array_key_exists($key, $keys)) { - yield $key => $value; - } + foreach ($collection as $key => $value) { + if (\array_key_exists($key, $keys)) { + yield $key => $value; } } } - ); + }; } } diff --git a/src/Operation/Pad.php b/src/Operation/Pad.php index 51932401d..bcb634e51 100644 --- a/src/Operation/Pad.php +++ b/src/Operation/Pad.php @@ -25,24 +25,22 @@ public function __construct(int $size, $value) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$size, $value] = $this->parameters; - return $collection::with( - static function () use ($size, $value, $collection): \Generator { - $y = 0; + return static function () use ($size, $value, $collection): \Generator { + $y = 0; - foreach ($collection as $key => $item) { - ++$y; + foreach ($collection as $key => $item) { + ++$y; - yield $key => $item; - } + yield $key => $item; + } - while ($y++ < $size) { - yield $value; - } + while ($y++ < $size) { + yield $value; } - ); + }; } } diff --git a/src/Operation/Pluck.php b/src/Operation/Pluck.php index 1b5f0bbbb..22d91adac 100644 --- a/src/Operation/Pluck.php +++ b/src/Operation/Pluck.php @@ -28,20 +28,18 @@ public function __construct($key, $default) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$key, $default] = $this->parameters; $operation = $this; - return $collection::with( - static function () use ($key, $default, $collection, $operation) { - $key = \is_string($key) ? \explode('.', \trim($key, '.')) : $key; + return static function () use ($key, $default, $collection, $operation) { + $key = \is_string($key) ? \explode('.', \trim($key, '.')) : $key; - foreach ($collection as $item) { - yield $operation->pick($collection, $item, $key, $default); - } + foreach ($collection as $item) { + yield $operation->pick($collection, $item, $key, $default); } - ); + }; } /** diff --git a/src/Operation/Prepend.php b/src/Operation/Prepend.php index ce84cd391..b3ca96060 100644 --- a/src/Operation/Prepend.php +++ b/src/Operation/Prepend.php @@ -14,20 +14,18 @@ final class Prepend extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$items] = $this->parameters; - return $collection::with( - static function () use ($items, $collection): \Generator { - foreach ($items as $item) { - yield $item; - } + return static function () use ($items, $collection): \Generator { + foreach ($items as $item) { + yield $item; + } - foreach ($collection as $item) { - yield $item; - } + foreach ($collection as $item) { + yield $item; } - ); + }; } } diff --git a/src/Operation/Proxy.php b/src/Operation/Proxy.php index c3db9f857..33edc231d 100644 --- a/src/Operation/Proxy.php +++ b/src/Operation/Proxy.php @@ -20,19 +20,13 @@ final class Proxy extends Operation */ public function __construct(string $method, $proxyMethod, ...$parameters) { - $params = [ - 'method' => $method, - 'proxyMethod' => $proxyMethod, - 'parameters' => $parameters, - ]; - - parent::__construct(...\array_values($params)); + parent::__construct(...[$method, $proxyMethod, $parameters]); } /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$method, $proxyMethod, $parameters] = $this->parameters; @@ -64,12 +58,10 @@ public function run(BaseCollectionInterface $collection): BaseCollectionInterfac throw new \InvalidArgumentException(\sprintf('Method %s does not exist.', $method)); } - return $collection::with( - static function () use ($reflection, $method, $collection, $callback) { - return $reflection - ->getMethod($method) - ->invoke($collection, $callback); - } - ); + return static function () use ($reflection, $method, $collection, $callback) { + return $reflection + ->getMethod($method) + ->invoke($collection, $callback); + }; } } diff --git a/src/Operation/Range.php b/src/Operation/Range.php index 9078dc02c..d7079a533 100644 --- a/src/Operation/Range.php +++ b/src/Operation/Range.php @@ -26,16 +26,14 @@ public function __construct(int $start, $end, $step) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$start, $end, $step] = $this->parameters; - return $collection::with( - static function () use ($start, $end, $step) { - for ($current = $start; $current < $end; $current += $step) { - yield $current; - } + return static function () use ($start, $end, $step) { + for ($current = $start; $current < $end; $current += $step) { + yield $current; } - ); + }; } } diff --git a/src/Operation/Rebase.php b/src/Operation/Rebase.php index f1210cc03..2c6d5b22a 100644 --- a/src/Operation/Rebase.php +++ b/src/Operation/Rebase.php @@ -14,8 +14,10 @@ final class Rebase extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { - return $collection::with($collection); + return static function () use ($collection) { + yield from $collection; + }; } } diff --git a/src/Operation/Run.php b/src/Operation/Run.php index 8efc4d027..16f024744 100644 --- a/src/Operation/Run.php +++ b/src/Operation/Run.php @@ -42,8 +42,14 @@ public function run(BaseCollectionInterface $collection) * @return mixed * The operation result. */ - private function dorun(BaseCollectionInterface $collection, Operation $operation) + private function doRun(BaseCollectionInterface $collection, Operation $operation) { - return $operation->run($collection); + $return = $operation->run($collection); + + if ($return instanceof \Closure) { + $return = $collection::with($return); + } + + return $return; } } diff --git a/src/Operation/Skip.php b/src/Operation/Skip.php index d3490d97e..6761fe1a8 100644 --- a/src/Operation/Skip.php +++ b/src/Operation/Skip.php @@ -24,27 +24,25 @@ public function __construct(int ...$skip) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$counts] = $this->parameters; - return $collection::with( - static function () use ($counts, $collection): \Generator { - $iterator = $collection->getIterator(); - $counts = \array_sum($counts); + return static function () use ($counts, $collection): \Generator { + $iterator = $collection->getIterator(); + $counts = \array_sum($counts); - foreach ($iterator as $key => $item) { - if (0 < $counts--) { - continue; - } - - break; + foreach ($iterator as $key => $item) { + if (0 < $counts--) { + continue; } - if ($iterator->valid()) { - yield from $iterator; - } + break; + } + + if ($iterator->valid()) { + yield from $iterator; } - ); + }; } } diff --git a/src/Operation/Slice.php b/src/Operation/Slice.php index 8cb0b9bc3..7ae3e9e67 100644 --- a/src/Operation/Slice.php +++ b/src/Operation/Slice.php @@ -14,18 +14,16 @@ final class Slice extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$offset, $length] = $this->parameters; - return $collection::with( - static function () use ($offset, $length, $collection): \Generator { - if (null === $length) { - yield from (new Skip($offset))->run($collection); - } else { - yield from (new Limit($length))->run((new Skip($offset))->run($collection)); - } + return static function () use ($offset, $length, $collection): \Generator { + if (null === $length) { + yield from (new Skip($offset))->run($collection)(); + } else { + yield from (new Limit($length))->run($collection::with((new Skip($offset))->run($collection)))(); } - ); + }; } } diff --git a/src/Operation/Sort.php b/src/Operation/Sort.php index 86c0679a6..92a62978d 100644 --- a/src/Operation/Sort.php +++ b/src/Operation/Sort.php @@ -4,7 +4,6 @@ namespace drupol\collection\Operation; -use drupol\collection\Collection; use drupol\collection\Contract\BaseCollection as BaseCollectionInterface; /** @@ -27,18 +26,16 @@ public function __construct(callable $callback) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$callback] = $this->parameters; - return $collection::with( - static function () use ($callback, $collection): \Generator { - $array = \iterator_to_array($collection); + return static function () use ($callback, $collection): \Generator { + $array = \iterator_to_array($collection); - \uasort($array, $callback); + \uasort($array, $callback); - yield from $array; - } - ); + yield from $array; + }; } } diff --git a/src/Operation/Walk.php b/src/Operation/Walk.php index 719e42641..1b467449a 100644 --- a/src/Operation/Walk.php +++ b/src/Operation/Walk.php @@ -24,20 +24,18 @@ public function __construct(callable ...$callbacks) /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$callbacks] = $this->parameters; - return $collection::with( - static function () use ($callbacks, $collection): \Generator { - $callback = static function ($carry, $callback) { - return $callback($carry); - }; + return static function () use ($callbacks, $collection): \Generator { + $callback = static function ($carry, $callback) { + return $callback($carry); + }; - foreach ($collection as $key => $value) { - yield $key => \array_reduce($callbacks, $callback, $value); - } + foreach ($collection as $key => $value) { + yield $key => \array_reduce($callbacks, $callback, $value); } - ); + }; } } diff --git a/src/Operation/Zip.php b/src/Operation/Zip.php index 425291bb8..70a7f2c0c 100644 --- a/src/Operation/Zip.php +++ b/src/Operation/Zip.php @@ -14,29 +14,33 @@ final class Zip extends Operation /** * {@inheritdoc} */ - public function run(BaseCollectionInterface $collection): BaseCollectionInterface + public function run(BaseCollectionInterface $collection): \Closure { [$iterables] = $this->parameters; - return $collection::with( - static function () use ($iterables, $collection): \Generator { - $getIteratorCallback = static function ($iterable) use ($collection) { - return $collection::with($iterable)->getIterator(); - }; + return static function () use ($iterables, $collection): \Generator { + $getIteratorCallback = static function ($iterable) use ($collection) { + return $collection::with($iterable)->getIterator(); + }; - $iterators = (new Walk($getIteratorCallback))->run( - (new Append(\array_merge([$collection], $iterables)))->run($collection::with()) - ); + $iterators = $collection::with((new Walk($getIteratorCallback))->run( + $collection::with((new Append(\array_merge([$collection], $iterables)))->run($collection::with())) + )); + + while ((new Contains(true))->run($collection::with((new Proxy('map', 'valid'))->run($iterators)))) { + yield $collection::with((new Proxy('map', 'current'))->run($iterators)); - while ((new Contains(true))->run((new Proxy('map', 'valid'))->run($iterators))) { - yield (new Proxy('map', 'current'))->run($iterators); - $iterators = (new Proxy('map', static function (\Iterator $i) { + $proxy = new Proxy( + 'map', + static function (\Iterator $i) { $i->next(); return $i; - }))->run($iterators); - } + } + ); + + $iterators = $collection::with($proxy->run($iterators)); } - ); + }; } }