Skip to content

Commit

Permalink
Merge pull request #321 from coady/master
Browse files Browse the repository at this point in the history
Sliding_window simplified and optimized.
  • Loading branch information
eriknw committed Aug 10, 2016
2 parents 72c3615 + e80450f commit 6b981ef
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
2 changes: 2 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ Joe Jevnik [@llllllllll](https://github.com
Rory Kirchner [@roryk](https://github.com/roryk)

[Steven Cutting](http://steven-cutting.github.io) [@steven_cutting](https://github.com/steven-cutting)

Aric Coady [@coady](https://github.com/coady)
12 changes: 2 additions & 10 deletions toolz/itertoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,16 +666,8 @@ def sliding_window(n, seq):
>>> list(map(mean, sliding_window(2, [1, 2, 3, 4])))
[1.5, 2.5, 3.5]
"""
it = iter(seq)
# An efficient FIFO data structure with maximum length
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)
yield tuple(d)
return zip(*(collections.deque(itertools.islice(it, i), 0) or it
for i, it in enumerate(itertools.tee(seq, n))))


no_pad = '__no__pad__'
Expand Down

0 comments on commit 6b981ef

Please sign in to comment.