From: Raymond Hettinger on
On Mar 22, 7:45 am, kj <no.em...(a)please.post> wrote:
> I have a list of items L, and a test function is_invalid that checks
> the validity of each item.  To check that there are no invalid
> items in L, I could check the value of any(map(is_invalid, L)).
> But this approach is suboptimal in the sense that, no matter what
> L is, is_invalid will be executed for all elements of L, even though
> the value returned by any() is fully determined by the first True
> in its argument.  In other words, all calls to is_invalid after
> the first one to return True are superfluous.  Is there a
> short-circuiting counterpart to any(map(is_invalid, L)) that avoids
> these superfluous calls?
>
> OK, there's this one, of course:
>
> def _any_invalid(L):
>     for i in L:
>         if is_invalid(i):
>             return True
>     return False  
>
> But is there anything built-in?  (I imagine that a lazy version of
> map *may* do the trick, *if* any() will let it be lazy.)

Yes, that will work:

from itertools import imap # lazy version of map
any(imap(is_invalid, L) # short-circuits on first True

Yet another approach (slightly faster):

from itertools import ifilter
any(ifilter(is_invalid, L))


Raymond



From: kj on
In <291d82b7-b13b-4f49-901c-8194f3e07598(a)e7g2000yqf.googlegroups.com> nn <pruebauno(a)latinmail.com> writes:

>If you are in Python 3 "any(map(is_invalid, L))" should short circuit.
>If you are in Python 2 use "from itertools import imap;
>any(imap(is_invalid, L))"

Thanks! I'm glad to know that one can get the short circuiting
using a map-type idiom. (I prefer map over comprehensions when I
don't need to define a function just for the purpose of passing it
to it.)

And thanks also to the other repliers for pointing out that the
comprehension version does what I was asking for.

~K
From: Tim Golden on
On 22/03/2010 18:30, kj wrote:
> Thanks! I'm glad to know that one can get the short circuiting
> using a map-type idiom. (I prefer map over comprehensions when I
> don't need to define a function just for the purpose of passing it
> to it.)

In what way does "map" over "comprehensions" save you defining a function?

any (map (is_invalid, L))
any (is_invalid (i) for i in L)

TJG

From: kj on
In <mailman.1069.1269283393.23598.python-list(a)python.org> Tim Golden <mail(a)timgolden.me.uk> writes:

>On 22/03/2010 18:30, kj wrote:
>> Thanks! I'm glad to know that one can get the short circuiting
>> using a map-type idiom. (I prefer map over comprehensions when I
>> don't need to define a function just for the purpose of passing it
>> to it.)

>In what way does "map" over "comprehensions" save you defining a function?

>any (map (is_invalid, L))
>any (is_invalid (i) for i in L)

I was talking in the *general* case. map at the very least requires
a lambda expression, which is a one-time function defintion.

~K
From: Steven D'Aprano on
On Mon, 22 Mar 2010 22:19:57 +0000, kj wrote:

> In <mailman.1069.1269283393.23598.python-list(a)python.org> Tim Golden
> <mail(a)timgolden.me.uk> writes:
>
>>On 22/03/2010 18:30, kj wrote:
>>> Thanks! I'm glad to know that one can get the short circuiting using
>>> a map-type idiom. (I prefer map over comprehensions when I don't need
>>> to define a function just for the purpose of passing it to it.)
>
>>In what way does "map" over "comprehensions" save you defining a
>>function?
>
>>any (map (is_invalid, L))
>>any (is_invalid (i) for i in L)
>
> I was talking in the *general* case. map at the very least requires a
> lambda expression, which is a one-time function defintion.

But keep in mind that instead of this:


map(lambda x,y: x+y, somelist)

you can do this:

import operator
map(operator.add, somelist)


In any case, the once-off cost of creating or importing a function is
usually quite cheap. As usual, the best advise is not to worry about
optimization until you have profiled the code and learned where the
actual bottlenecks are. Write what reads best, not what you guess might
be faster, until you really know you need the speed and that it is an
optimization and not a pessimation.



--
Steven