[Python-Dev] Efficient predicates for the standard library
Jeremy Fincher
fincher.8 at osu.edu
Sun Oct 5 00:26:48 EDT 2003
On Saturday 04 October 2003 07:40 pm, Christian Stork wrote:
> I'd like to advocate the inclusion of efficient (ie iterator-based)
> predicates to the standard library.
I agree. At the very least, I think such predicates should be in the
itertools module.
> My reasoning is that these predicate should be used in many places,
> especially as part of assert statements.
One of the places where I use them most, to be sure :)
> def all(pred, seq):
> "Returns True if pred(x) is True for every element in the iterable"
> return False not in imap(pred, seq)
>
> def some(pred, seq):
> "Returns True if pred(x) is True at least one element in the iterable"
> return True in imap(pred, seq)
>
> def no(pred, seq):
> "Returns True if pred(x) is False for every element in the iterable"
> return True not in imap(pred, seq)
I would instead call some "any" (it's more standard among the functional
languages I've worked with), and I wouldn't bother with "no," since it's
exactly the same as "not any" (or "not some," as the case may be).
As Raymond Hettinger already mentioned, obviously such predicates over
sequences should exhibit short-circuit behavior -- any should return with the
first True response and all should return with the first False response.
Jeremy
More information about the Python-Dev
mailing list