Skip to content

Commit ca21311

Browse files
committed
refactor: speed up group_by, by using deque to avoid explicit for loop
1 parent a26afea commit ca21311

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/superstream/stream.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ def sum(self, start: T = 0) -> T:
5757

5858
def group_by(self, classifier: Callable[[T], K]) -> Dict[K, List[T]]:
5959
groups = {}
60-
for i in self._stream:
60+
def _classify(i):
6161
groups.setdefault(classifier(i), []).append(i)
62+
deque(map(_classify, self._stream), maxlen=0)
6263
return groups
6364

6465
def reduce(self, func: Callable[[T, T], T], initial: T = _initial_missing) -> Optional[T]:

0 commit comments

Comments
 (0)