Skip to content

Commit

Permalink
Update Group operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Aug 27, 2020
1 parent 18a7885 commit 6fce398
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
8 changes: 4 additions & 4 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -978,14 +978,14 @@ public function it_can_group()
10 => ['h'],
]);

$callback = static function ($key, $value) {
return $value % 2;
$callback = static function (int $value, int $key) {
return 0 === ($value % 2) ? 'even' : 'odd';
};

$this::fromIterable(range(0, 20))
->group($callback)
->shouldIterateAs([
0 => [
'even' => [
0,
2,
4,
Expand All @@ -998,7 +998,7 @@ public function it_can_group()
18,
20,
],
1 => [
'odd' => [
1,
3,
5,
Expand Down
6 changes: 4 additions & 2 deletions src/Operation/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(?callable $callable = null)
* @return mixed
* @psalm-return TKey
*/
static function ($key, $value) {
static function ($value, $key) {
return $key;
};
}
Expand Down Expand Up @@ -59,8 +59,10 @@ static function (Iterator $iterator, callable $callable): Generator {
* @psalm-return array<TKey, list<T>>
*/
static function (array $collect, $value, $key) use ($callable): array {
if (null !== $groupKey = $callable($key, $value)) {
if (null !== $groupKey = $callable($value, $key)) {
$collect[$groupKey][] = $value;
} else {
$collect[$key] = $value;
}

return $collect;
Expand Down

0 comments on commit 6fce398

Please sign in to comment.