Skip to content

Commit

Permalink
Minor change - Update operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 22, 2019
1 parent 0add9fc commit 38d8f71
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ final class Count extends Operation
*/
public function run(BaseCollectionInterface $collection)
{
return \iterator_count($collection->getIterator());
return \iterator_count($collection);
}
}
2 changes: 1 addition & 1 deletion src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function run(BaseCollectionInterface $collection): BaseCollectionInterfac
return $collection::with(
static function () use ($callbacks, $collection): \Generator {
foreach ($callbacks as $callback) {
foreach ($collection->getIterator() as $key => $value) {
foreach ($collection as $key => $value) {
if (true === (bool) $callback($value, $key)) {
yield $key => $value;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ public function run(BaseCollectionInterface $collection): BaseCollectionInterfac

return $collection::with(
static function () use ($depth, $collection): \Generator {
$iterator = $collection->getIterator();

foreach ($iterator as $item) {
foreach ($collection as $item) {
if (!\is_array($item) && !$item instanceof BaseCollectionInterface) {
yield $item;
} elseif (1 === $depth) {
Expand Down
18 changes: 7 additions & 11 deletions src/Operation/Last.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ final class Last extends Operation
*/
public function run(BaseCollectionInterface $collection)
{
/** @var \Iterator $iterator */
$iterator = $collection->getIterator();

$last = $iterator->current();

while (true === $iterator->valid()) {
$last = $iterator->current();
$iterator->next();
}

return $last;
return (
new Reduce(
static function ($carry, $item) {
return $item;
},
$collection->getIterator()->current()
))->run($collection);
}
}
2 changes: 1 addition & 1 deletion src/Operation/Rebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ final class Rebase extends Operation
*/
public function run(BaseCollectionInterface $collection): BaseCollectionInterface
{
return $collection::with($collection->getIterator());
return $collection::with($collection);
}
}
2 changes: 1 addition & 1 deletion src/Operation/Sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function run(BaseCollectionInterface $collection): BaseCollectionInterfac

return $collection::with(
static function () use ($callback, $collection): \Generator {
$array = \iterator_to_array($collection->getIterator());
$array = \iterator_to_array($collection);

\uasort($array, $callback);

Expand Down

0 comments on commit 38d8f71

Please sign in to comment.