Re: builtin functions for and and or?

2005-02-13 Thread Nick Coghlan
Roose wrote: It would be a lot simpler if it was included in the distribution. I would be willing to add it (even though it is completely trivial). I think it would go fine in itertools (I would even put them as builtins, but I'm not going to go there because probably not everyone uses it as ofte

Re: builtin functions for and and or?

2005-02-13 Thread Michael Spencer
Roose wrote: Previous discussion on this topic: http://groups-beta.google.com/group/comp.lang.python/msg/a76b4c2caf6c435c Michael OK, well then. That's really the exact same thing, down to the names of the functions. So what ever happened to that? I don't recall: probably http://www.google.c

Re: builtin functions for and and or?

2005-02-13 Thread Roose
> Previous discussion on this topic: > http://groups-beta.google.com/group/comp.lang.python/msg/a76b4c2caf6c435c > > Michael > OK, well then. That's really the exact same thing, down to the names of the functions. So what ever happened to that? That was over a year ago! I don't see any mention

Re: builtin functions for and and or?

2005-02-13 Thread Michael Spencer
Roose wrote: Yeah, as we can see there are a million ways to do it. But none of them are as desirable as just having a library function to do the same thing. I'd argue that since there are so many different ways, we should just collapse them into one: any() and all(). That is more in keeping wit

Re: builtin functions for and and or?

2005-02-13 Thread Roose
Yeah, as we can see there are a million ways to do it. But none of them are as desirable as just having a library function to do the same thing. I'd argue that since there are so many different ways, we should just collapse them into one: any() and all(). That is more in keeping with the python

Re: builtin functions for and and or?

2005-02-13 Thread Roose
"Diez B. Roggisch" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > So usually I just write a little function any( L, boolean_function = > > identity ) or all( ... ). But I am kind of sick of doing that all the > > time -- does it exist anywhere in the Python libraries? It seems r

Re: builtin functions for and and or?

2005-02-13 Thread Brian Beck
George Sakkis wrote: You're right, it doesn't short circuit, as most of the examples posted above. Here's one that it does: ... I also looked into taking advantage of itertools' dropwhile, but the all and any recipes included in the itertools documentation do short-circuit and don't require the

Re: builtin functions for and and or?

2005-02-13 Thread bearophileHUGS
In Python there are so many ways to do things... This looks like another one, I haven't tested it: not False in imap(pred, iterable) As usual tests are required to measure the faster one. I agree with Roose, there are are some "primitive" operations (like this, and flatten, partition, mass remova

Re: builtin functions for and and or?

2005-02-13 Thread George Sakkis
"Roose" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I need this a lot: a one line way to do a n-ary and or 'or'. > > e.g., > > result = True > for x in L: > if not boolean_function(x): > result = False > > or > > >>> reduce(operator.__and__, [boolean_function(x) for x in L)

Re: builtin functions for and and or?

2005-02-13 Thread Brian Beck
Steven Bethard wrote: Another alternative: not False in (bool(x) for x in L) Note that this should short-circuit, where min won't. Steve Whoops, for some reason the thought that short-circuiting didn't apply to And entered my mind while trying to post a nice solution. Hard to say why considering

Re: builtin functions for and and or?

2005-02-13 Thread John Machin
Michael Hartl wrote: > I warmly recommend downloading Peter Norvig's Python utilities file > (http://aima.cs.berkeley.edu/python/utils.py) and putting it on your > Python path. (E.g., in bash, put a line like > > export PYTHONPATH="/path/to/utilities_directory" > > in your .bashrc file.) The uti

Re: builtin functions for and and or?

2005-02-13 Thread Steven Bethard
Brian Beck wrote: Roose wrote: I need this a lot: a one line way to do a n-ary and or 'or'. Here's a one-liner for the n-ary and: bool(min(bool(x) for x in L)) py> bool(min(bool(x) for x in [1, 1, 1, 0])) False py> bool(min(bool(x) for x in [1, 1, 1, 1])) True py> bool(min(bool(x) for x in ['a', ''

Re: builtin functions for and and or?

2005-02-13 Thread Brian Beck
Roose wrote: I need this a lot: a one line way to do a n-ary and or 'or'. Here's a one-liner for the n-ary and: bool(min(bool(x) for x in L)) py> bool(min(bool(x) for x in [1, 1, 1, 0])) False py> bool(min(bool(x) for x in [1, 1, 1, 1])) True py> bool(min(bool(x) for x in ['a', '', 'b', 'c'])) Fals

Re: builtin functions for and and or?

2005-02-13 Thread Brian Beck
Brian Beck wrote: def all(seq, pred=bool): "Returns True if pred(x) is True for every element in the iterable" for elem in ifilterfalse(pred, seq): return False return True def any(seq, pred=bool): "Returns True if pred(x) is True for at least one element in the iterable"

Re: builtin functions for and and or?

2005-02-13 Thread Steven Bethard
Roose wrote: I need this a lot: a one line way to do a n-ary and or 'or'. e.g., result = True for x in L: if not boolean_function(x): result = False or reduce(operator.__and__, [boolean_function(x) for x in L) Can you use itertools? py> def boolfn(x): ... print "boolfn: %r" % x ... re

Re: builtin functions for and and or?

2005-02-13 Thread Brian Beck
Roose wrote: I need this a lot: a one line way to do a n-ary and or 'or'. Looks like there are itertools recipes for those, similar to what Michael just posted. Taken from here: http://docs.python.org/lib/itertools-recipes.html def all(seq, pred=bool): "Returns True if pred(x) is True for ev

Re: builtin functions for and and or?

2005-02-13 Thread Michael Hartl
I warmly recommend downloading Peter Norvig's Python utilities file (http://aima.cs.berkeley.edu/python/utils.py) and putting it on your Python path. (E.g., in bash, put a line like export PYTHONPATH="/path/to/utilities_directory" in your .bashrc file.) The utils.py file defines many useful fun

Re: builtin functions for and and or?

2005-02-13 Thread Diez B. Roggisch
> So usually I just write a little function any( L, boolean_function = > identity ) or all( ... ). But I am kind of sick of doing that all the > time -- does it exist anywhere in the Python libraries? It seems really > common to me. Put things into your own module and add it to your python path.

builtin functions for and and or?

2005-02-13 Thread Roose
I need this a lot: a one line way to do a n-ary and or 'or'. e.g., result = True for x in L: if not boolean_function(x): result = False or >>> reduce(operator.__and__, [boolean_function(x) for x in L) So usually I just write a little function any( L, boolean_function = identity ) or all(