Stream deduplicate
This commit is contained in:
@@ -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))
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user