[Python-Dev] Re: Quantifiers Was: Trinary Operators
Raymond Hettinger
python@rcn.com
Sat, 8 Feb 2003 00:04:22 -0500
[Oren Tirosh]
> > > Oh dear. If you are really going to do these these cutesy English-like
> > > expressions I'd have to resurrect my old proposal of:
> > >
> > > if CONDITION for some NAME in ITERABLE:
> > > if CONDITION for every NAME in ITERABLE:
> > > if CONDITION for no NAME in ITERABLE:
[Samuele Pedroni]
> I would not dislike to have expression forms equivalent to applications of
>
> def every(pred,l):
> for x in l:
> if not pred(x): return False
> return True
>
> def some(pred,l):
> for x in in l:
> if pred(x): return True
> return False
>
> def such(pred,l,otherwise=None):
> for x in in l:
> if pred(x): return x
> return otherwise
>
> as list comprehension is equivalent to map and filter.
BTW, quantifiers can be efficiently implemented
using functions in the itertools module. I'll add an
example to the docs.
Raymond Hettinger