Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Add median and mode functions to collections #14305

Merged
merged 2 commits into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,67 @@ public function average($key = null)
return $this->avg($key);
}

/**
* Get the median of a given key.
*
* @param null $key
* @return mixed|null
*/
public function median($key = null)
{
$count = $this->count();
if ($count == 0) {
return;
}

$collection = isset($key) ? $this->map(function ($value) use ($key) {
return $value->{$key};
}) : $this;

$values = $collection->values()->sort();
$middlePosition = (int) floor($count / 2);
$hasAnOddQuantity = $count % 2;

if ($hasAnOddQuantity) {
return $values->get($middlePosition);
}

$start = $values->get($middlePosition - 1);
$end = $values->get($middlePosition);

return (new static([$start, $end]))->average();
}

/**
* Get the mode of a given key.
*
* @param null $key
* @return static|null
*/
public function mode($key = null)
{
$count = $this->count();
if ($count == 0) {
return;
}

$collection = isset($key) ? $this->map(function ($value) use ($key) {
return $value->{$key};
}) : $this;

$set = new static;
$collection->each(function ($value) use (&$set) {
$set[$value] = isset($set[$value]) ? $set[$value] + 1 : 1;
});

$sorted = $set->sort();
$highestValue = $sorted->last();

return $sorted->filter(function ($value) use ($highestValue) {
return $value == $highestValue;
})->sort()->keys();
}

/**
* Collapse the collection of items into a single array.
*
Expand Down
62 changes: 62 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,68 @@ public function testPipe()
return $collection->sum();
}));
}

public function testMedianValueWithArrayCollection()
{
$collection = new Collection([1, 2, 2, 4]);

$this->assertEquals(2, $collection->median());
}

public function testMedianValueByKey()
{
$collection = new Collection([
(object) ['foo' => 1],
(object) ['foo' => 2],
(object) ['foo' => 2],
(object) ['foo' => 4],
]);
$this->assertEquals(2, $collection->median('foo'));
}

public function testEvenMedianCollection()
{
$collection = new Collection([
(object) ['foo' => 0],
(object) ['foo' => 3],
]);
$this->assertEquals(1.5, $collection->median('foo'));
}

public function testMedianOnEmptyCollectionReturnsNull()
{
$collection = new Collection();
$this->assertNull($collection->median());
}

public function testModeOnNullCollection()
{
$collection = new Collection();
$this->assertNull($collection->mode());
}

public function testMode()
{
$collection = new Collection([1, 2, 3, 4, 4, 5]);
$this->assertEquals(new Collection([4]), $collection->mode());
}

public function testModeValueByKey()
{
$collection = new Collection([
(object) ['foo' => 1],
(object) ['foo' => 1],
(object) ['foo' => 2],
(object) ['foo' => 4],
]);
$this->assertEquals(new Collection([1]), $collection->mode('foo'));
}

public function testWithMultipleModeValues()
{
$collection = new Collection([1, 2, 2, 1]);
$this->assertEquals(new Collection([1, 2]), $collection->mode());
}
}

class TestAccessorEloquentTestStub
Expand Down