From: kj on 22 Mar 2010 10:45 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.) TIA! ~K From: Tim Golden on 22 Mar 2010 10:57 On 22/03/2010 14:45, kj 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.) Have I missed the point of your question, perhaps? This seems to work as lazily as you'd like... def less_than_five (x): print "testing", x return x < 5 L = range (10) print any (less_than_five (i) for i in L) print all (less_than_five (i) for i in L) # for symmetry TJG From: Jean-Michel Pichavant on 22 Mar 2010 11:08 kj 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.) > > TIA! > > ~K > Sounds like unnecessary optimization. Just write def _any_valid(L): return bool([i for i in L if is_valid(i)]) If you really care about speed, meaning if the user experiences some execution duration increase, then the solution you proposed is fine. JM From: Tim Wintle on 22 Mar 2010 11:12 On Mon, 2010-03-22 at 14:45 +0000, kj 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, any( is_invalid(a) for a in L ) .... generator expression will be lazily computed. Tim From: nn on 22 Mar 2010 11:17 kj 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.) > > TIA! > > ~K 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))"  |  Next  |  Last