@@ -211,7 +211,7 @@ public function map(callable $mapper): static
211
211
* @template U
212
212
*
213
213
* @param callable(T):U $mapper
214
- * @param bool $boolExpr
214
+ * @param bool $boolExpr
215
215
*
216
216
* @return static
217
217
*/
@@ -228,7 +228,7 @@ public function mapIf(callable $mapper, bool $boolExpr): static
228
228
* @template U
229
229
*
230
230
* @param callable(T):U $mapper
231
- * @param DataStream $stream
231
+ * @param DataStream $stream
232
232
*
233
233
* @return static
234
234
*/
@@ -245,7 +245,7 @@ public function mapTo(callable $mapper, self $stream): static
245
245
* Mapping values to MapStream
246
246
*
247
247
* @param callable(array|mixed): array{string,mixed} $mapper
248
- * @param MapStream|null $new
248
+ * @param MapStream|null $new
249
249
*
250
250
* @return MapStream
251
251
*/
@@ -264,7 +264,7 @@ public function mapToMap(callable $mapper, MapStream $new = null): MapStream
264
264
* Mapping values to IntStream
265
265
*
266
266
* @param callable(T):int $mapper
267
- * @param IntStream|null $new
267
+ * @param IntStream|null $new
268
268
*
269
269
* @return IntStream
270
270
*/
@@ -282,7 +282,7 @@ public function mapToInt(callable $mapper, IntStream $new = null): IntStream
282
282
* Mapping values to StringStream
283
283
*
284
284
* @param callable(T):string $mapper
285
- * @param StringStream|null $new
285
+ * @param StringStream|null $new
286
286
*
287
287
* @return StringStream
288
288
*/
@@ -528,7 +528,7 @@ public function forEach(callable $handler): void
528
528
529
529
/**
530
530
* @param callable(array|mixed, int|string): array $func
531
- * @param array $arr
531
+ * @param array $arr
532
532
*
533
533
* @return array
534
534
*/
@@ -544,7 +544,7 @@ public function eachToArray(callable $func, array $arr = []): array
544
544
* Each item to map
545
545
*
546
546
* @param callable(array|mixed): array{string, mixed} $func
547
- * @param array $map
547
+ * @param array $map
548
548
*
549
549
* @return array<string, mixed> return the map key and value
550
550
*/
@@ -559,15 +559,51 @@ public function eachToMap(callable $func, array $map = []): array
559
559
}
560
560
561
561
/**
562
- * @param callable $handler
563
- * @param ...$args
562
+ * Collect data items to list
564
563
*
565
- * @return mixed
564
+ * @param callable(mixed): mixed $func
565
+ * @param array $list
566
+ *
567
+ * @return array return an array: [item, ...]
568
+ */
569
+ public function collectToList (callable $ func , array $ list = []): array
570
+ {
571
+ foreach ($ this as $ item ) {
572
+ $ list [] = $ func ($ item );
573
+ }
574
+ return $ list ;
575
+ }
576
+
577
+ /**
578
+ * Collect data items to map
579
+ *
580
+ * @param callable $func
581
+ * @param array $map
582
+ *
583
+ * @return array<string, mixed> return a map: [key => item]
584
+ */
585
+ public function collectToMap (callable $ func , array $ map = []): array
586
+ {
587
+ return $ this ->eachToMap ($ func , $ map );
588
+ }
589
+
590
+ /**
591
+ * Group data by key
592
+ *
593
+ * @param callable $getKeyFn
594
+ *
595
+ * @return array
566
596
*/
567
- public function collect (callable $ handler , ... $ args ): mixed
597
+ public function groupBy (callable $ getKeyFn ): array
568
598
{
569
- // TODO
570
- return null ;
599
+ $ map = [];
600
+ foreach ($ this as $ item ) {
601
+ $ key = $ getKeyFn ($ item );
602
+ // collect to map
603
+ $ map [$ key ][] = $ item ;
604
+ }
605
+
606
+ return $ map ;
571
607
}
572
608
573
609
/**
0 commit comments