Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster unique, isdistinct, merge_sorted, and sliding_window. #178

Merged
merged 1 commit into from
May 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
'sliding_window', 'partition', 'partition_all', 'count', 'pluck')


identity = lambda x: x


def remove(predicate, seq):
""" Return those items of sequence for which predicate(item) is False

Expand Down Expand Up @@ -120,7 +117,9 @@ def _merge_sorted_key(seqs, key):
heapq.heapify(pq)

# Repeatedly yield and then repopulate from the same iterator
while True:
heapreplace = heapq.heapreplace
heappop = heapq.heappop
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man, I never would have thought of this.

while len(pq) > 1:
try:
while True:
# raises IndexError when pq is empty
Expand All @@ -129,11 +128,15 @@ def _merge_sorted_key(seqs, key):
item = next(it) # raises StopIteration when exhausted
s[0] = key(item)
s[2] = item
heapq.heapreplace(pq, s) # restore heap condition
heapreplace(pq, s) # restore heap condition
except StopIteration:
heapq.heappop(pq) # remove empty iterator
except IndexError:
return
heappop(pq) # remove empty iterator
if pq:
# Much faster when only a single iterable remains
_, itnum, item, it = pq[0]
yield item
for item in it:
yield item


def interleave(seqs, pass_exceptions=()):
Expand Down Expand Up @@ -161,7 +164,7 @@ def interleave(seqs, pass_exceptions=()):
iters = newiters


def unique(seq, key=identity):
def unique(seq, key=None):
""" Return only unique elements of a sequence

>>> tuple(unique((1, 2, 3)))
Expand All @@ -175,11 +178,18 @@ def unique(seq, key=identity):
('cat', 'mouse')
"""
seen = set()
for item in seq:
tag = key(item)
if tag not in seen:
seen.add(tag)
yield item
seen_add = seen.add
if key is None:
for item in seq:
if item not in seen:
seen_add(item)
yield item
else: # calculate key
for item in seq:
val = key(item)
if val not in seen:
seen_add(val)
yield item


def isiterable(x):
Expand Down Expand Up @@ -214,10 +224,11 @@ def isdistinct(seq):
"""
if iter(seq) is seq:
seen = set()
seen_add = seen.add
for item in seq:
if item in seen:
return False
seen.add(item)
seen_add(item)
return True
else:
return len(seq) == len(set(seq))
Expand Down Expand Up @@ -528,9 +539,10 @@ def sliding_window(n, seq):
d = collections.deque(itertools.islice(it, n), n)
if len(d) != n:
raise StopIteration()
d_append = d.append
for item in it:
yield tuple(d)
d.append(item)
d_append(item)
yield tuple(d)


Expand Down
7 changes: 6 additions & 1 deletion toolz/tests/test_itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from functools import partial
from toolz.itertoolz import (remove, groupby, merge_sorted,
concat, concatv, interleave, unique,
identity, isiterable,
isiterable,
mapcat, isdistinct, first, second,
nth, take, drop, interpose, get,
rest, last, cons, frequencies,
Expand All @@ -14,6 +14,10 @@
from operator import add, mul


def identity(x):
return x


def iseven(x):
return x % 2 == 0

Expand Down Expand Up @@ -54,6 +58,7 @@ def test_merge_sorted():
assert ''.join(merge_sorted('abc', 'abc', 'abc', key=ord)) == 'aaabbbccc'
assert ''.join(merge_sorted('cba', 'cba', 'cba',
key=lambda x: -ord(x))) == 'cccbbbaaa'
assert list(merge_sorted([1], [2, 3, 4], key=identity)) == [1, 2, 3, 4]


def test_interleave():
Expand Down