File tree Expand file tree Collapse file tree 2 files changed +11
-19
lines changed Expand file tree Collapse file tree 2 files changed +11
-19
lines changed Original file line number Diff line number Diff line change 11
11
12
12
namespace _ ;
13
13
14
+ use function _ \internal \createAggregator ;
15
+
14
16
/**
15
17
* Creates an array composed of keys generated from the results of running
16
18
* each element of `collection` through `iteratee`. The corresponding value of
33
35
* // => ['3' => 2, '5' => 1]
34
36
* </code>
35
37
*/
36
- function countBy (iterable $ collection , callable $ iteratee = null ): array
38
+ function countBy (iterable $ collection , callable $ iteratee ): array
37
39
{
38
- if (!$ iteratee ) {
39
- $ iteratee = '_\identity ' ;
40
- }
41
-
42
- $ result = [];
43
-
44
- foreach ($ collection as $ value ) {
45
- if (!\is_scalar ($ value )) {
46
- continue ;
40
+ return createAggregator (function ($ result , $ key , $ value ) {
41
+ if (!isset ($ result [$ value ])) {
42
+ $ result [$ value ] = 0 ;
47
43
}
48
44
49
- $ computed = $ iteratee ($ value );
50
-
51
- if (!isset ($ result [$ computed ])) {
52
- $ result [$ computed ] = 0 ;
53
- };
54
-
55
- $ result [$ computed ]++;
56
- }
45
+ $ result [$ value ]++;
57
46
58
- return $ result ;
47
+ return $ result ;
48
+ })($ collection , $ iteratee );
59
49
}
Original file line number Diff line number Diff line change 11
11
12
12
namespace _ ;
13
13
14
+ use function _ \internal \createAggregator ;
15
+
14
16
/**
15
17
* Creates an array composed of keys generated from the results of running
16
18
* each element of `collection` through `iteratee`. The order of grouped values
You can’t perform that action at this time.
0 commit comments