Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * Fix styling * wip * wip * wip * wip Co-authored-by: freekmurze <freekmurze@users.noreply.github.com>
- Loading branch information
1 parent
0692d4b
commit 93ce541
Showing
6 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Spatie\CollectionMacros\Macros; | ||
|
||
class WeightedRandom | ||
{ | ||
public function __invoke() | ||
{ | ||
return function (callable|string $weightAttribute, $default = null) { | ||
if (is_string($weightAttribute)) { | ||
$attributeName = $weightAttribute; | ||
|
||
$weightAttribute = function ($item) use ($attributeName) { | ||
return data_get($item, $attributeName); | ||
}; | ||
} | ||
|
||
$range = 0; | ||
|
||
$weightedItems = collect($this->items) | ||
->map(function ($item) use ($weightAttribute, &$range) { | ||
$weightAttribute = $weightAttribute($item); | ||
$range += $weightAttribute; | ||
|
||
return [ | ||
'range' => $range, | ||
'weight' => $weightAttribute, | ||
'item' => $item, | ||
]; | ||
}) | ||
->filter(function (array $weightedItem) { | ||
return $weightedItem['weight'] > 0; | ||
}); | ||
|
||
if ($weightedItems->isEmpty()) { | ||
return $this->random(); | ||
} | ||
|
||
|
||
$randomNumber = rand(1, $range); | ||
|
||
$itemAndRange = $weightedItems | ||
->first(function ($weightedItem) use ($randomNumber) { | ||
return $weightedItem['range'] >= $randomNumber; | ||
}); | ||
|
||
return $itemAndRange['item'] ?? $default; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Spatie\CollectionMacros\Test\Macros; | ||
|
||
use Illuminate\Support\Collection; | ||
use Spatie\CollectionMacros\Test\TestCase; | ||
|
||
class WeightedRandomTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_will_probably_return_the_heaviest_item_most() | ||
{ | ||
$items = collect([ | ||
['value' => 'a', 'weight' => 1], | ||
['value' => 'b', 'weight' => 10], | ||
['value' => 'c', 'weight' => 1], | ||
]); | ||
|
||
$mostPopularValue = Collection::range(0, 1000) | ||
->map(function () use ($items) { | ||
return $items->weightedRandom(function (array $item) { | ||
return $item['weight']; | ||
}); | ||
}) | ||
->groupBy('value') | ||
->map | ||
->count() | ||
->sortDesc() | ||
->flip() | ||
->first(); | ||
|
||
$this->assertEquals('b', $mostPopularValue); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_not_pick_a_value_without_a_weight() | ||
{ | ||
$items = collect([ | ||
['value' => 'a', 'weight' => 0], | ||
['value' => 'b', 'weight' => 0], | ||
['value' => 'c', 'weight' => 1], | ||
['value' => 'c', 'weight' => 0], | ||
]); | ||
|
||
$pickedItem = $items->weightedRandom(fn (array $item) => $item['weight']); | ||
|
||
$this->assertEquals('c', $pickedItem['value']); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_pick_a_random_value_when_all_values_are_zero() | ||
{ | ||
$items = collect([ | ||
['value' => 'a', 'weight' => 0], | ||
['value' => 'b', 'weight' => 0], | ||
['value' => 'c', 'weight' => 0], | ||
]); | ||
|
||
$this->assertIsArray($items->weightedRandom(fn (array $item) => $item['weight'])); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_pick_a_weighted_random_by_attribute_name() | ||
{ | ||
$items = collect([ | ||
['value' => 'a', 'weight' => 0], | ||
['value' => 'b', 'weight' => 1], | ||
['value' => 'c', 'weight' => 0], | ||
]); | ||
|
||
$item = ($items->weightedRandom('weight')); | ||
|
||
$this->assertEquals('b', $item['value']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters