Re: andmap and ormap

2006-03-14 Thread Peter Otten
Peter Otten wrote: > def andmap(predicate, items): > return False not in (predicate(item) for item in items) > > def ormap(predicate, items): > return True in (predicate(items) for item in items) These are both broken because they imply the test (in e. g. ormap) if True == predi

Re: andmap and ormap

2006-03-14 Thread Scott David Daniels
[EMAIL PROTECTED] wrote: > The following works perfectly: > import operator > def andmap(b,L): > return reduce(operator.and_, [b(x) for x in L]) > def ormap(b,L): > return reduce(operator.or_, [b(x) for x in L]) Note your [b(x) for x in L] evaluates b(x) for all elements of L before you b

Re: andmap and ormap

2006-03-14 Thread Terry Reedy
"Joel Hedlund" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >> footnote: if you have a recent Python 2.5 build, > > Who would have that? Is it a good idea to use a pre-alpha python version? The pre-public release version compiles as 'alpha0'. I have the impression the current al

Re: andmap and ormap

2006-03-14 Thread wkehowski
The following works perfectly: import operator def andmap(b,L): return reduce(operator.and_, [b(x) for x in L]) def ormap(b,L): return reduce(operator.or_, [b(x) for x in L]) Thanks! -- http://mail.python.org/mailman/listinfo/python-list

Re: andmap and ormap

2006-03-14 Thread Peter Otten
Peter Otten wrote: > def ormap(predicate, items): > return True in (predicate(items) for item in items) should be def ormap(predicate, items): return True in (predicate(item) for item in items) Hmmpf. Peter -- http://mail.python.org/mailman/listinfo/python-list

Re: andmap and ormap

2006-03-14 Thread Georg Brandl
Peter Otten wrote: > Python 2.5 will feature similar functions any() and all() which seem to have > a fixed predicate == bool, though. You cannot write all(predicate, list) but all(predicate(x) for x in list) Georg -- http://mail.python.org/mailman/listinfo/python-list

Re: andmap and ormap

2006-03-14 Thread Georg Brandl
Joel Hedlund wrote: >> footnote: if you have a recent Python 2.5 build, > > Who would have that? Is it a good idea to use a pre-alpha python version? > Or any unrealeased python version for that matter? I was under the impression > that > the recommended way to go for meager developers in python

Re: andmap and ormap

2006-03-14 Thread Peter Otten
[EMAIL PROTECTED] wrote: > def ormap(b,L): > if True in map(b,L): return True > else: return False > > Is this good enough? No, because - (as Felipe observed) it doesn't shortcut, i. e. it always evaluates b(item) for all items in L. - it creates a temporary list - if truthvalue: return T

Re: andmap and ormap

2006-03-14 Thread Joel Hedlund
> footnote: if you have a recent Python 2.5 build, Who would have that? Is it a good idea to use a pre-alpha python version? Or any unrealeased python version for that matter? I was under the impression that the recommended way to go for meager developers in python like myself is to stick with

Re: andmap and ormap

2006-03-14 Thread Fredrik Lundh
Felipe Almeida Lessa wrote: > The problem is that it will evaluate all possibilities needlessly. Try > (not tested and 2.4-only): footnote: if you have a recent Python 2.5 build, you can make them even shorter: >>> help(any) any(...) any(iterable) -> bool Return True if bool(x) is True f

Re: andmap and ormap

2006-03-14 Thread Diez B. Roggisch
> Does python have andmap and ormap: > > andmap((lambda t: boolean(t)),L) > > gives True if boolean(t) is True for all t in L and False otherwise? > And > > ormap((lambda t: boolean(t)),L) > > gives True if boolean(t) is True for some t in L and False o

Re: andmap and ormap

2006-03-14 Thread Dan Sommers
On 14 Mar 2006 04:23:55 -0800, [EMAIL PROTECTED] wrote: > Hello, > Does python have andmap and ormap: > andmap((lambda t: boolean(t)),L) > gives True if boolean(t) is True for all t in L and False otherwise? > And > ormap((lambda t: boolean(t)),L) > gives True if boolean(

Re: andmap and ormap

2006-03-14 Thread Felipe Almeida Lessa
Em Ter, 2006-03-14 às 04:23 -0800, [EMAIL PROTECTED] escreveu: > def andmap(b,L): > if False in map(b,L): return False > else: return True > > def ormap(b,L): > if True in map(b,L): return True > else: return False > > Is this good enough? The problem is that it will evaluate all possibi

andmap and ormap

2006-03-14 Thread wkehowski
Hello, Does python have andmap and ormap: andmap((lambda t: boolean(t)),L) gives True if boolean(t) is True for all t in L and False otherwise? And ormap((lambda t: boolean(t)),L) gives True if boolean(t) is True for some t in L and False otherwise? One can use a list comprehension like [x