diff --git a/src/morgslib/morgslib.py b/src/morgslib/morgslib.py index f5b727e..2300ab4 100644 --- a/src/morgslib/morgslib.py +++ b/src/morgslib/morgslib.py @@ -56,6 +56,9 @@ class Stream(Generic[T]): out[fxn(item)].append(item) return Stream(out.items()) + def deduplicate(self) -> Stream[T]: + return Stream(dict.fromkeys(self.iter)) + def apply(self, fxn: Callable[[Iterator[T]], Iterable[Tn]|Tn]) -> Stream[Tn]: return Stream(fxn(self.iter)) diff --git a/src/morgslib/msl_tests.py b/src/morgslib/msl_tests.py index f4cedeb..5c0fbef 100644 --- a/src/morgslib/msl_tests.py +++ b/src/morgslib/msl_tests.py @@ -21,6 +21,9 @@ class TestStringMethods(unittest.TestCase): def test_group(self): self.assertEqual(Stream([1, 2, 3, 4, 5]).group(lambda x: x%3).to(dict), {0: [3], 1: [1, 4], 2: [2, 5]}) + def test_deduplicate(self): + self.assertEqual(Stream([1, 2, 3, 2, 5, 3, 4]).deduplicate().to(list), [1, 2, 3, 5, 4]) + def test_apply_single(self): self.assertEqual(Stream([1, 2, 3, 4, 5]).apply(sum).to_single(int), 15) def test_apply_multi(self):