Stream deduplicate

This commit is contained in:
2026-01-29 17:23:17 -06:00
parent 9483cf0531
commit 7bec51fc84
2 changed files with 6 additions and 0 deletions

View File

@@ -56,6 +56,9 @@ class Stream(Generic[T]):
out[fxn(item)].append(item) out[fxn(item)].append(item)
return Stream(out.items()) 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]: def apply(self, fxn: Callable[[Iterator[T]], Iterable[Tn]|Tn]) -> Stream[Tn]:
return Stream(fxn(self.iter)) return Stream(fxn(self.iter))

View File

@@ -21,6 +21,9 @@ class TestStringMethods(unittest.TestCase):
def test_group(self): 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]}) 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): def test_apply_single(self):
self.assertEqual(Stream([1, 2, 3, 4, 5]).apply(sum).to_single(int), 15) self.assertEqual(Stream([1, 2, 3, 4, 5]).apply(sum).to_single(int), 15)
def test_apply_multi(self): def test_apply_multi(self):