@@ -546,26 +546,6 @@ def count(self) -> int:
546
546
"""
547
547
return self ._finish (count (self ._data ), close_source = True )
548
548
549
- def counted_groups (
550
- self : Stream [T ], key : Callable [[T ], object ] | None = None
551
- ) -> Stream [tuple [T , int ]]:
552
- """Group the stream by a key and count the items in the group.
553
-
554
- >>> Stream("abba").counted_groups().starmap(print).for_each()
555
- a 1
556
- b 2
557
- a 1
558
- >>> Stream("aaabbbbc").counted_groups(ord).starmap(print).for_each()
559
- a 3
560
- b 4
561
- c 1
562
- """
563
-
564
- def _map (_ : object , g : Iterator [T ]) -> tuple [T , int ]:
565
- return (next (g ), 1 + count (g ))
566
-
567
- return Stream (itertools .starmap (_map , itertools .groupby (self , key )))
568
-
569
549
def dedup (self , * , key : None | Callable [[T ], object ] = None ) -> Self :
570
550
"""Remove consecutive equal values.
571
551
@@ -595,6 +575,25 @@ def dedup(self, *, key: None | Callable[[T], object] = None) -> Self:
595
575
)
596
576
return self
597
577
578
+ def dedup_counting (self ) -> Stream [tuple [T , int ]]:
579
+ """Group the stream and count the items in the group.
580
+
581
+ >>> Stream("abba").dedup_counting().starmap(print).for_each()
582
+ a 1
583
+ b 2
584
+ a 1
585
+ >>> Stream("AaaaBBcccc").dedup_counting().starmap(print).for_each()
586
+ A 1
587
+ a 3
588
+ B 2
589
+ c 4
590
+ """
591
+
592
+ def _map (k : T , g : Iterator [T ]) -> tuple [T , int ]:
593
+ return (k , count (g ))
594
+
595
+ return Stream (itertools .starmap (_map , itertools .groupby (self )))
596
+
598
597
@override
599
598
def distinct (self , * , use_set : bool = True ) -> Self :
600
599
"""Remove duplicate values.
0 commit comments