Skip to content

Commit

Permalink
Distinct now maintains order (EntilZha#151)
Browse files Browse the repository at this point in the history
* Distinct now maintains order.

* Tests now pass Black

* making distinct more explicit and lazy
  • Loading branch information
stephan-rayner authored Dec 16, 2020
1 parent 1377414 commit dcaac91
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 7 additions & 4 deletions functional/test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,17 @@ def test_reverse(self):
self.assert_type(result)

def test_distinct(self):
l = [1, 1, 2, 3, 2, 3]
expect = [1, 2, 3]
l = [1, 3, 1, 2, 2, 3]
expect = [1, 3, 2]
s = self.seq(l)
result = s.distinct()
self.assertEqual(result.size(), len(expect))
for er in zip(expect, result):
self.assertEqual(
er[0], er[1], "Order was not preserved after running distinct!"
)
for e in result:
self.assertTrue(e in expect)
result = s.distinct()
self.assertEqual(result.size(), len(expect))
self.assert_type(result)

def test_distinct_by(self):
Expand Down
7 changes: 6 additions & 1 deletion functional/transformations.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ def distinct_t():
"""

def distinct(sequence):
return iter(set(sequence))
seen = set()
for element in sequence:
if element in seen:
continue
seen.add(element)
yield element

return Transformation("distinct", distinct, None)

Expand Down

0 comments on commit dcaac91

Please sign in to comment.