Skip to content

Commit

Permalink
use builtin functions where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp authored and JulienPalard committed Oct 29, 2011
1 parent a294569 commit 6d8974a
Showing 1 changed file with 4 additions and 29 deletions.
33 changes: 4 additions & 29 deletions pipe.py 100755 → 100644
Expand Up @@ -430,18 +430,12 @@ def skip(iterable, qte):
@Pipe
def all(iterable, pred):
"Returns True if ALL elements in the given iterable are true for the given pred function"
for x in iterable:
if not pred(x):
return False
return True
return builtins.all(pred(x) for x in iterable)

@Pipe
def any(iterable, pred):
"Returns True if ANY element in the given iterable is True for the given pred function"
for x in iterable:
if pred(x):
return True
return False
return builtins.any(pred(x) for x in iterable)

@Pipe
def average(iterable):
Expand Down Expand Up @@ -480,27 +474,8 @@ def as_dict(iterable):
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = list(range(n))
cycles = list(range(n, n-r, -1))
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] -= 1
if cycles[i] == 0:
indices[i:] = indices[i+1:] + indices[i:i+1]
cycles[i] = n - i
else:
j = cycles[i]
indices[i], indices[-j] = indices[-j], indices[i]
yield tuple(pool[i] for i in indices[:r])
break
else:
return
for x in itertools.permutations(iterable, r):
yield x

@Pipe
def netcat(to_send, host, port):
Expand Down

0 comments on commit 6d8974a

Please sign in to comment.