Skip to content

Commit

Permalink
refactor: Optimize Group operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 3, 2021
1 parent d7cd376 commit a4b2653
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 4 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,10 @@ public function it_can_group(): void
1 => [0 => 'b', 1 => 'b'],
2 => [0 => 'c', 1 => 'c'],
]);

$this::empty()
->group()
->shouldIterateAs([]);
}

public function it_can_groupBy(): void
Expand Down
12 changes: 7 additions & 5 deletions src/Operation/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ static function (Iterator $iterator): Generator {
$last = [];

foreach ($iterator as $current) {
if (current($last) === $current) {
$last[] = $current;
if ([] === $last) {
$last = [$current];

continue;
}

if (current($last) === false) {
$last = [$current];
if (current($last) === $current) {
$last[] = $current;

continue;
}
Expand All @@ -49,7 +49,9 @@ static function (Iterator $iterator): Generator {
$last = [$current];
}

yield $last;
if ([] !== $last) {
yield $last;
}
};
}
}

0 comments on commit a4b2653

Please sign in to comment.