Skip to content

Commit

Permalink
Merge pull request #9 from jbvsmo/patch-1
Browse files Browse the repository at this point in the history
Python 3 compatibility and reduced memory footprint on tail
  • Loading branch information
JulienPalard committed Feb 12, 2012
2 parents 6d8974a + 78ef8a8 commit 208b669
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions pipe.py
Expand Up @@ -314,7 +314,7 @@ def stdout(x):
t
Like Haskell's operator ":"
>>> 0 | t(1) | t(2) == range(3)
>>> 0 | t(1) | t(2) == range(3) | as_list
True
to_type
Expand Down Expand Up @@ -415,9 +415,13 @@ def take(iterable, qte):
@Pipe
def tail(iterable, qte):
"Yield qte of elements in the given iterable."
for item in (iterable | as_list)[-qte:]:
yield item

out = []
for item in iterable:
out.append(item)
if len(out) > qte:
out.pop(0)
return out

@Pipe
def skip(iterable, qte):
"Skip qte elements in the given iterable, then yield others."
Expand Down Expand Up @@ -608,7 +612,7 @@ def run_with(iterable, func):

@Pipe
def t(iterable, y):
if hasattr(iterable,'__iter__'):
if hasattr(iterable,'__iter__') and not isinstance(iterable, str):
return iterable + type(iterable)([y])
else:
return [iterable, y]
Expand Down

0 comments on commit 208b669

Please sign in to comment.