11
$\begingroup$

I have a relatively simple bit of code that looks like the following:

---some code here---
If[statement, Abort[]];
---more code following---

However, while $Aborted appears as output, the evaluation of the rest of the cell does not abort, continuing execution of code beyond the If statement. How do I actually get the running cell to cease evaluating the rest of the cell?

Edit: It seems that one way to accomplish this is to wrap the entire cell in parenthesis, e.g., but this is a fairly ugly solution, especially with auto-formatting turned on (my cell has many lines of code, and the parenthesis are huuuuuge). Anyone have a more elegant solution?

(
---some code here---
If[statement, Abort[]];
---more code following---
)
$\endgroup$

2 Answers 2

12
$\begingroup$

This question has been asked before on stack overflow. However we will summerize some of the answers given there on our new Mathematica site.

Wrap In Compound Expression

One suggestion by Michael Pilat, given there was to wrap your lines in CompoundExpression, e.g.

(
   Print@Range[5];
   Abort[];
   Print@Range[5];
)

During evaluation of In[39]:= {1,2,3,4,5}

$Aborted

If you mind the formatting of parenthesis, you could explicitly use the FullForm of CompoundExpression like this:

CompoundExpression[
   Print@Range[5],
   Abort[],
   Print@Range[5],
]

Use of $PreRead

Suggested by Alexey Popkov:

In[1]:= $new$PreRead = False;
AbortAllPendingInputs := 
  AbortProtect[If[! $new$PreRead, $new$PreRead = True;
    $TimeOfAbort = SessionTime[];
    last$PreRead = ToString[Definition[$PreRead], InputForm];
    ClearAll[$PreRead];
    $PreRead := If[TrueQ[SessionTime[] - $TimeOfAbort < 1], "",
       $new$PreRead = False;
       ClearAll[$PreRead];
       If[last$PreRead === "Null", #, 
        ToExpression[last$PreRead]; $PreRead@#]
       ] &;]];

In[3]:= f := CheckAbort[Pause[10], AbortAllPendingInputs; Abort[]]

In[4]:= While[True, f]
While[True, f]
While[True, f]

Out[4]= $Aborted

Note that this solution will need to be modified if you use $PreRead for anything else.

Use of CellEvaluationFunction

CellEvaluationFunction gets the BoxData expression, before it is even split. Just assigning something like CellEvaluationFunction:>ToExpression seems to do the trick.

You can set it as an Input cell style, or as a notebook option, or front end session option, or however you like

SetOptions[$FrontEndSession, CellEvaluationFunction :> ToExpression]

However, you lose the multiple outputs in those new lines when you don't use ;. This can probably be solved but I don't know very well how CellEvalutionFunction works, and how can it receive a box structure, default to Identity but expect to return an expression. Edit should you come up with any improvements

$\endgroup$
9
  • $\begingroup$ Since you tagged this community wiki, would you mind if others were to port over some of the suggestions from the stack overflow question here? With appropriate credit and whatnot of course! $\endgroup$
    – nixeagle
    Mar 28, 2012 at 21:52
  • $\begingroup$ @nixeagle No, of course I don't mind! That was the idea. $\endgroup$ Mar 28, 2012 at 21:55
  • $\begingroup$ Alright, I'll go ahead and work on that. Plus I ought to suggest to @Guillochon that using more smaller functions might help with the concern raised in his Edit. $\endgroup$
    – nixeagle
    Mar 28, 2012 at 21:56
  • 1
    $\begingroup$ Alright, done editing this. If anyone else can make the answer clearer or wants to volunteer a 3rd (possibly better!) solution, please do so. As a side note, as I don't know where else to mention this, do we want to keep that abort tag on this question? $\endgroup$
    – nixeagle
    Mar 28, 2012 at 22:06
  • $\begingroup$ @LeonidShifrin, I just saw that your answer was a community wiki and that in the link you provided someone suggests in comments a CellEvaluationFunction approach, like my answer. Should I delete it and put it in the community answer? $\endgroup$
    – Rojo
    Mar 31, 2012 at 18:23
0
$\begingroup$

Note that the documentation FrontEnd uses CellEvaluationFunction -> Identity and not ToExpression. Still the example works and

Print[a]; Abort[]; Print[b]

aborts properly (not printing b) because all statements are in one line. Apparently it is the line breaks which break up the cell into individual commands, not the semicolons. So there must be another way "fixing" this.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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