[Python-Dev] Efficient predicates for the standard library
M.-A. Lemburg
mal at lemburg.com
Wed Oct 8 12:32:37 EDT 2003
Christian Stork wrote:
> The examples given in itertools' documentation are a good starting
> point. More specifically I'm talking about the following:
>
>
> 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)
FYI, similar APIs have been part of mxTools for years, except
that they are called exists() and forall() (the terms used in
math for these things), plus there are a few more:
count(condition,sequence)
Counts the number of objects in sequence for which condition returns
true and returns the result as integer. condition must be a callable
object.
exists(condition,sequence)
Return 1 if and only if condition is true for at least one of the
items in sequence and 0 otherwise. condition must be a callable object.
forall(condition,sequence)
Return 1 if and only if condition is true for all of the items in
sequence and 0 otherwise. condition must be a callable object.
index(condition,sequence)
Return the index of the first item for which condition is true.
A ValueError is raised in case no item is found. condition must be
a callable object.
Note that the signatures are similar to those of map(), filter()
and reduce() and the do truth checking rather than compare
True/False to the result value which is useful sometimes.
mxTools currently does not support iterable objects for
sequence, but that should be easy to add.
More's here:
http://www.egenix.com/files/python/mxTools.html
--
Marc-Andre Lemburg
eGenix.com
Professional Python Software directly from the Source (#1, Oct 08 2003)
>>> Python/Zope Products & Consulting ... http://www.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::
More information about the Python-Dev
mailing list