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.
Answer
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.
Comments
Post a Comment