Reliable clean-up in Mathematica

后端 未结 3 1861
迷失自我
迷失自我 2020-12-24 08:52

For better or worse, Mathematica provides a wealth of constructs that allow you to do non-local transfers of control, including Return, Catch/Throw, Abort and Goto. However,

3条回答
  •  一整个雨季
    2020-12-24 09:30

    Michael Pilat provided the key trick for "catching" returns, but I ended up using it in a slightly different way, using the fact that Return forces the return value of a named function as well as control structures like Do. I made the expression that is being cleaned up after into the down-value of a local symbol, like so:

    Attributes[CleanUp] = {HoldAll};
    CleanUp[expr_, form_] :=
      Module[{body, value, aborted = False},
    
       body[] := expr;
    
       Catch[
        CheckAbort[
         value = body[],
         aborted = True];
        form;
        If[aborted,
         Abort[],
         value],
        _, (form; Throw[##]) &]];
    

提交回复
热议问题