I am running Mathematica 7. The following is admittedly a silly example, but suppose that I have a list list whose entries are either True or False. Suppose that I have some function truthFun[] which evaluates randomly to either True or False. I would like to call truthFun[] and print False until truthFun[] evaluates to True for the first time.
One way to do this is using a For loop. We can exit the loop "early" using Break[]. (As the documentation states, Break[] exits the nearest enclosing Do, For, or While loop.) For example, the following works:
truthFun[] := RandomChoice[{True, False}] For[i = 1, i <= 10^8, i++, If[truthFun[] === False, Print[False], Break[]]]; Print["Finished"] False
False
Finished
However, it is often recommended that Mathematica users avoid using loops (which are relatively slow in Mathematica, I think). Is it possible to accomplish the above using Table, which is probably faster?
I tried the following, but unfortunately it appears that Break[] does not work with Table:
truthFun[] := RandomChoice[{True, False}] Table[If[truthFun[] === False, Print[False], Break[]], {i, 1, 10^8}]; False
False
False
Hold[Break[]]
Also, Mathematica gives me this error message:
Break::nofwd: No enclosing
For,While, orDofound forBreak[].
Is there an analogue to Break in Table? Thanks for your time.
Map[Print, First@Split@list]$\endgroup$Tableso far, have a look at my implementation of abortable table here. It can be easily modified to work withBreakor any other custom form of interruption, rather thanAbort[]. $\endgroup$Scanto go over the table, andReturnorThrowto exit on condition. If the latter, enclose the Scan in aCatch. $\endgroup$Return[Null, Table]or the equivalentReturn[,Table]to get behavior similar toBreakwithout the syntax error highlighting. $\endgroup$