forked from muglug/psl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgroup_by.php
62 lines (55 loc) · 1.53 KB
/
group_by.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
declare(strict_types=1);
namespace Psl\Dict;
use Psl;
use Psl\Type;
/**
* Returns a new array where
* - keys are the results of the given function called on the given values.
* - values are an array of original values that all produced the same key.
*
* If a value produces a null key, it's omitted from the result.
*
* Example:
*
* Dict\group_by(
* [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
* fn($i) => $i < 2 ? null : $i + 5
* )
* => Dict(
* 7 => [2], 8 => [3], 9 => [4], 10 => [5], 11 => [6], 12 => [7], 13 => [8], 14 => [9], 15 => [10]
* )
*
* Dict\group_by(
* [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
* fn($i) => $i < 2 ? null : ($i >= 7 ? 12 : $i +5)
* )
* => Dict(7 => [2], 8 => [3], 9 => [4], 10 => [5], 11 => [6], 12 => [7, 8, 9, 10])
*
* @template Tk of array-key
* @template Tv
*
* @param iterable<Tv> $values
* @param (callable(Tv): ?Tk) $key_func
*
* @return array<Tk, list<Tv>>
*/
function group_by(iterable $values, callable $key_func): array
{
$result = [];
foreach ($values as $value) {
$key = $key_func($value);
if (null === $key) {
continue;
}
Psl\invariant(
Type\array_key()->matches($key),
'Expected $key_func to return a value of type array-key, value of type (%s) returned.',
gettype($key)
);
/** @var Tk $key */
$result[$key] = $result[$key] ?? [];
$result[$key][] = $value;
}
return $result;
}