diff --git a/README.md b/README.md index ba7b651..12543e7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ # MORG Stream Library -This python library allows for the handling of data in a stream format. \ No newline at end of file +This python library allows for the handling of data in a stream format. + +The library may be installed from Avalon with: +``` +pip install git+https://git.icolotl.com/morgana/morgslib +``` \ No newline at end of file diff --git a/src/morgslib/morgslib.py b/src/morgslib/morgslib.py index ffdd0fd..c5aebd4 100644 --- a/src/morgslib/morgslib.py +++ b/src/morgslib/morgslib.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from collections import defaultdict from typing import Any, Callable, Iterable, Iterator, Self, TypeVar, Generic T = TypeVar('T') @@ -47,5 +48,14 @@ class Stream(Generic[T]): def first(self, fxn: Callable[[T], bool]) -> T | None: return next(filter(fxn, self.iter), None) + def groupby(self, fxn: Callable[[T], Tn]) -> Stream[tuple[Tn, list[T]]]: + out: defaultdict[Tn, list[T]] = defaultdict(list) + for item in self.iter: + out[fxn(item)].append(item) + return Stream(out.items()) + def to(self, fxn: Callable) -> Any: - return fxn(self.iter) \ No newline at end of file + return fxn(self.iter) + + def print(self, sep=","): + print(*self.iter, sep=sep) \ No newline at end of file