PEP 289: universal and existential operators

Alex Martelli aleax at aleax.it
Thu Nov 6 22:03:54 CET 2003


Michele Simionato wrote:

> austin at smartobject.biz (Jess Austin) wrote in message
> news:<b3ad1da7.0311060552.5a542cf4 at posting.google.com>...
>> It seems like the consensus is "all" and "any".
> 
> Not necessarely. I prefer "alltrue" and "anytrue", for instance.

Me too, FWIW.  But more important is, where are these operators
going to _live_ -- the builtins are definitely _out_ , judging by
Guido's recent reactions.

I think we need a companion module to itertools.  The functions
in itertools *produce* useful iterators; we need a module for
functions that usefully *consume* iterators.  Yes, a few, in each
case, judged more useful than others, are built-ins (iter,
enumerate, reversed as producers; sum, min, and max as consumers).

But, most others won't be -- so we have itertools and need [good 
name for "iterator consumers" sought -- accumulators?).  This, btw,
will usefully afford e.g.
from accumulators import any as anytrue
or viceversa, mitigating the effect of a dlsliked name being chosen.


Other candidates (names can surely be enhanced...) in random order:

average(numbers)
average_and_variance(numbers)      # returns tuple -- average, variance
median(numbers)

highest(N, numbers)                # return list of N highest numbers
lowest(N, numbers)                 #        ditto for lowest
# note: highest and lowest might get optional key= -- like lists' sort
#       method has added in 2.4

take(N, numbers)                   #        ditto for the first N
exists(xs)                         # true if xs is non-empty

repeatcall(f, xs, result=None)
# like: for x in xs: f(x)
        return result
# typical uses:
# # just concatenate a lots of sequences into list allitems
# allitems = []
# repeatcall(allitems.extend, lotsofsequences)
# # merge a lot of dictionaries and loop on merged result dict d
# d = {}
# for k in repeatcall(d.update, ds, d): ...


Alex





More information about the Python-list mailing list