MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: "short circuiting" And and Or


Szabolcs Horvat wrote:

>I'd like to write a function that tests whether there are any elements
>in a list that violate a certain condition. I could simply use
>And@@condition/@list, but for reasons of efficiency I'd like to stop
>the testing as soon as a "False" value is found. Is there any elegant
>way of doing this without writing an explicit While loop?
>
>Szabolcs Horvát
>
Here is one idea:

ReleaseHold[
    And @@ (Hold[Positive[#]]& /@ {1,1,-1,1,-1,1})
]

False

Here is one way to check that testing is stopped as soon as a False 
value is found:

ReleaseHold[
    And @@ (Hold[Print[#];Positive[#]]& /@ {1,1,-1,1,-1,-1,1})
]

1
1
-1
False

Carl Woll
Wolfram Research


  • Prev by Date: Re: "short circuiting" And and Or
  • Next by Date: RE: Problem in loading packages in Mathematica
  • Previous by thread: Re: "short circuiting" And and Or
  • Next by thread: Re: "short circuiting" And and Or