# And@@(f/@list) without evaluating f for the whole list

And[f[1],f[2]] doesn't evaluate f[2] if f[1] is false, but And@@{f[1],f[2]} does.

How do I evaluate And@@(f/@list), or Or@@(f/@list) for that matter, without evaluating more than necessary of f/@list?

• AllTrue[list, f] ? Commented Mar 15, 2020 at 14:39
• Related: (124534) Commented Mar 16, 2020 at 2:01
• AllTrue is cleaner, but for reference: And @@ f /@ Hold @@ list Commented Mar 16, 2020 at 2:38

How about using Block?:

f[a_] := a < 2
lst = Range[5]
Block[{f}, And @@ f /@ lst]


If you already have the explicit list at hand, you can simply use Unevaluated:

And @@ Unevaluated @ {f[1], f[2], f[3], f[4], f[5]}


If you don't even want to generate the expression And[f[1], f[2], …] in intermediate step, then I think it's better to avoid And:

Catch[If[! f @ #, Throw @ False] & /@ lst; True]


Similarly, Or @@ f /@ lst can be implemented as

Catch[If[f @ #, Throw @ True] & /@ lst; False]


For completeness, as mentioned by ilian and Blrp, AllTrue and AnyTrue seems to be the best choice since v10.0.

A simple test for all the methods under this question:

f[a_] := a < 250000
f2 = # < 250000 &;
lst = Range[500000];

Block[{f}, And @@ f /@ lst] // RepeatedTiming
(* {0.29, False} *)
Block[{f}, And @@ f2 /@ lst] // RepeatedTiming
(* {0.290, False} *)

Catch[If[! f @ #, Throw @ False] & /@ lst; True] // RepeatedTiming
(* {0.488, False} *)
Catch[If[! f2@ #, Throw @ False] & /@ lst; True] // RepeatedTiming
(* {0.4785, False} *)

Catch[Scan[If[! f @ #, Throw @ False] &, lst]; True] // RepeatedTiming
(* {0.3554, False} *)
Catch[Scan[If[! f2 @ #, Throw @ False] &, lst]; True] // RepeatedTiming
(* {0.352, False} *)

And @@ f /@ Hold @@ lst // RepeatedTiming
(* {0.231, False} *)
And @@ f2 /@ Hold @@ lst // RepeatedTiming
(* {0.194, False} *)

AllTrue[lst,f] // RepeatedTiming
(* {0.165, False} *)
AllTrue[lst,f2] // RepeatedTiming
(* {0.13, False} *)


Tested on v12.1.0. (Wolfram cloud, Android app. )

• ilian's suggestion of AllTrue is another way, and most convenient in some circumstances such as built-in functions and unassigned pure functions. Add it, as well as AnyTrue, and I'll accept. Commented Mar 15, 2020 at 22:32
• @victor Seems that the new version is always released a bit earlier silently on Wolfram cloud. The blog post is coming soon, I believe. Commented Mar 16, 2020 at 5:19
• @xzczd that's cool - you can already browse the list of new feature etc. reference.wolframcloud.com/cloudplatform/guide/… Commented Mar 16, 2020 at 6:17
• In the Ctach[Map[...]] construct, use Scan instead of Map as it doesn't generate the intermediate List like Map. Commented Mar 16, 2020 at 12:57
• @rcollyer I used Map just for compactness, and didn't expect the generation of output slows down the code that much. Edited. Thx for the reminding. Commented Mar 17, 2020 at 2:01