Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In Python, there is a function all which returns true if all of its arguments are true, and any which returns true if at least one of its arguments is true. I find these quite useful in functional programming. Given how much Mathematica encourages functional programming constructs, I was surprised not to find anything equivalent to these all and any functions in the documentation. Does Mathematica have equivalents to these functions, or some standard way to achieve the same effect?

I can implement them myself as

AllOf[b_List] := Fold[And, True, b][[1]]
AllOf[b__] := Fold[And, True, {b}][[1]]

AnyOf[b_List] := Fold[Or, True, b][[1]]
AnyOf[b__] := Fold[Or, True, {b}][[1]]

(these may not be always correct, but they've worked for my purposes). But I would rather use something built-in if it exists.

share|improve this question
4  
Alternatively, AllOf[b_List] := And @@ b and AnyOf[b_List] := Or @@ b ... –  J. M. Jan 29 '12 at 4:03
3  
This question was asked before on SO several times: stackoverflow.com/questions/4181470/…, stackoverflow.com/questions/4911827/…, stackoverflow.com/questions/4867076/…. In my answers there, I also discuss the short-circuiting. –  Leonid Shifrin Jan 29 '12 at 10:18
    
Note, for more general forms (if the problem involves values other than True/False) you would probably want to use MemberQ and FreeQ. –  amr Jan 1 '13 at 8:37
add comment

5 Answers

up vote 29 down vote accepted

Both, And and Or should work for All and Any respectively. You may have to get creative in how you apply them, though. For instance,

And @@ {True, False, True}

works just like you would expect

AllOf @ {True, False, True}

to without any additional work. Similarly,

Or @@ {False, True, False}

works like AnyOf.

share|improve this answer
    
Huh, somehow I missed that And and Or can apply to multiple (>2) arguments. –  David Z Jan 29 '12 at 4:21
6  
It's interesting to mention that they are also short circuiting and have HoldAll. So False && expr will never evaluate expr. –  Szabolcs Jan 29 '12 at 9:07
    
@Szabolcs See my answer to this question: stackoverflow.com/questions/4911827/…, for a short-circuiting implementation. I also discuss short-curcuiting here: stackoverflow.com/questions/4867076/… –  Leonid Shifrin Jan 29 '12 at 10:20
    
If you want to benefit from short circuiting, while applying And to a List like in this answer, note that you have to use Unevaluated, i.e. Apply[And, Unevaluated[{False, expr}]] –  Jacob Akkerboom 6 hours ago
add comment

You can implement equivalents of the any and all functions in MATLAB and python in Mathematica using the MemberQ and FreeQ functions as:

any[x_List] := MemberQ[x, True]
all[x_List] := FreeQ[x, False]

For large lists, these will be about an order of magnitude faster in the worst case to several orders faster in the best case, when compared to the And and Or solutions.

share|improve this answer
2  
+1. I suspect that the speed advantage comes largely from the fact that in your functions, the clauses have been already evaluated, while And and Or evaluate them only if necessary. If you add the time it took to evaluate those, the difference might be not as large. OTOH, if, for a particular problem, you have some fast vectorized way to obtain the list of True/False, this may indeed be much faster than using And / Or. –  Leonid Shifrin Jan 29 '12 at 21:53
1  
This is really "lateral thinking" :). I always use MemberQ and FreeQ the other way around (ie using the "element" as the free var) –  belisarius Jul 24 '12 at 5:21
add comment

In addition to the simple form where you already have a list of True|False elements, you may want lazy evaluation in creating that list, short circuiting if the test fails. You can do this with Hold. I include a Print statement so that you can see what actually evaluates:

(Print@#; # != 0) & /@ Hold[1, 0, 0, 1, 1, 0, 1, 0]
And @@ %

(Print@#; # != 0) & /@ Hold[0, 0, 1, 1, 0, 1, 0]
Or @@ %

Another form that can come in handy if you have a function that uses two arguments is Inner:

a = {91, 95, 72, 90, 82, 97, 76, 81, 82, 70};
b = {7, 4, 3, 9, 1, 4, 5, 6, 5, 2};

Inner[(Print[##]; Divisible[##]) &, a, b, And]

Inner[(Print[##]; Divisible[##]) &, a, b, Or]
share|improve this answer
add comment

Another way for All is to use VectorQ function

VectorQ[lis, TrueQ]
share|improve this answer
add comment

The Wolfram Language and Mathematica 10 (available now on the Raspberry Pi) have new functions — AnyTrue, AllTrue, NoneTrue — which take a predicate and test any/all/none on the input list. For example:

AnyTrue[Range@5, EvenQ]
(* True *)

AllTrue[{True, False, False}, TrueQ] (* or Identity in place of TrueQ *)
(* False *)

These functions can also be turned into a predicate themselves by using just a test function as a single argument:

NoneTrue[StringQ]@{"a", 1, 23}
(* False *)
share|improve this answer
    
The latter one could use Identity in place of TrueQ, provided all elements are True or False. The AllTrue[EvenQ][{2, 4, 6}] syntax is interesting to show too. –  Szabolcs Mar 29 at 17:23
    
Yes, I'll add that syntax in as well. –  rm -rf Mar 29 at 17:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.