Skip to content

Commit

Permalink
Implement uniq
Browse files Browse the repository at this point in the history
  • Loading branch information
nykh committed Mar 28, 2018
1 parent 8ac364a commit 108d5ed
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ Or using decorators:
>>> [5, -4, 3, -2, 1] | sort(key=abs) | concat
'1, -2, 3, -4, 5'

dedup()
Deduplicate values

>>> [1,1,2,2,3,3,1,2,3] | uniq | as_list
[1, 2, 3,]

uniq()
Like dedup() but only deduplicate consecutive values.

>>> [1,1,2,2,3,3,1,2,3] | uniq | as_list
[1, 2, 3, 1, 2, 3]

reverse
Like Python's built-in "reversed" primitive.
>>> [1, 2, 3] | reverse | concat
Expand Down
20 changes: 16 additions & 4 deletions pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,27 @@ def skip(iterable, qte):

@Pipe
def dedup(iterable):
"""Only yield unique items. Rely on __hash__ of the item type to find duplication."""
"""Only yield unique items. Use a set to keep track of duplicate data."""
seen = set()
for item in iterable:
if item in seen:
continue
else:
if item not in seen:
seen.add(item)
yield item

@Pipe
def uniq(iterable):
"""Deduplicate consecutive duplicate values."""
iterator = iter(iterable)
try:
prev = next(iterator)
except StopIteration:
return []
yield prev
for item in iterator:
if item != prev:
yield item
prev = item

@Pipe
def all(iterable, pred):
"""Returns True if ALL elements in the given iterable are true for the
Expand Down

0 comments on commit 108d5ed

Please sign in to comment.