2
$\begingroup$

I've written some code that's been getting longer and longer, Is there a way to "hide" the section of the code that does not require input so that i can just give it my two inputs and jump straight to the results?

p1 = {-5, 4};
m = 9/8;

p2 = {First[Take[p1, 1]] + 1, First[Drop[p1, 1]] + m};
m = (Drop[p1, 1] - Drop[p2, 1])/(Take[p1, 1] - Take[p2, 1]);
StringForm["The slope is: `1`", First[m]]
bb = Solve[Drop[p1, 1] == m*Take[p1, 1] + b];
StringForm["The y intercept is `1`", First[bb[[All, 1, 2]]]]
StringForm["Slope Intercept form: y=`1`x`3``2`", First[m], 
 Abs[First[bb[[All, 1, 2]]]], 
 If[First[bb[[All, 1, 2]]] >= 0, "+", "-"]]
denom = Max[Denominator[m]];
StringForm["Standard Form: `1`x`4``3`y=`2`", 
 If[Max[m] < 0, -denom*First[m], denom*First[m]], 
 If[Max[m] > 0, -denom*First[bb[[All, 1, 2]]], 
  denom*First[bb[[All, 1, 2]]]], If[Max[m] > 0, -denom, denom], 
 If[Max[m] > 0, "", "+"]]
StringForm["General Form: `1`x`4``3`y`5``2`=0", 
 If[Max[m] < 0, -denom*First[m], 
  denom*First[m]], -If[Max[m] > 0, -denom*First[bb[[All, 1, 2]]], 
   denom*First[bb[[All, 1, 2]]]], If[Max[m] > 0, -denom, denom], 
 If[Max[m] > 0, "", "+"], 
 If[If[Max[m] < 0, -denom*First[m], denom*First[m]] > 0, "+", ""]]
StringForm["Point Slope Form: y`4``1`=`2`(x`5``3`)", 
 Abs[Max[Drop[p1, 1]]], Max[m], Abs[Max[Take[p1, 1]]], 
 If[Max[Drop[p1, 1]] >= 0, "-", "+"], 
 If[Max[Take[p1, 1]] >= 0, "-", "+"]]

I would like to hide or collapse the code after the first 2 lines and still be able to run it.

$\endgroup$
2
  • $\begingroup$ tutorial/DefiningFunctions ? $\endgroup$
    – Kuba
    Commented Mar 24, 2019 at 19:23
  • $\begingroup$ If you are using the Notebook Front End GUI you can insert a new cell, perhaps "Section" and give it a descprition. Place the code suggested by the goldberg answer in the next cell. Finally insert another new "Section" with a description and place the code that calls the previous code in the next cell. You can "collapse sections" and hide cells groups by enabling "Show open/close icon for cell groups" in the "Edit" > "Preferences" > "Interface" menu. $\endgroup$
    – Somos
    Commented Mar 24, 2019 at 21:40

1 Answer 1

2
$\begingroup$

Perhaps this will work for you.

Make the part of code you want to ignore into a command with SetDelayed ( := ), like so;

resuts := (
  p2 = {First[Take[p1, 1]] + 1, First[Drop[p1, 1]] + m0}; 
  m = (Drop[p1, 1] - Drop[p2, 1])/(Take[p1, 1] - Take[p2, 1]); 
  denom = Max[Denominator[mm]];
  bb = Solve[Drop[p1, 1] == m*Take[p1, 1] + b];
  Column[
    {StringForm["The slope is: `1`", First[m]],
     StringForm["The y intercept is `1`", First[bb[[All, 1, 2]]]],
     StringForm["Slope Intercept form: y=`1`x`3``2`", 
       First[m], Abs[First[bb[[All, 1, 2]]]], 
       If[First[bb[[All, 1, 2]]] >= 0, "+", "-"]],
     StringForm["Standard Form: `1`x`4``3`y=`2`", 
       If[Max[m] < 0, -denom*First[m], denom*First[m]], 
       If[Max[m] > 0, -denom*First[bb[[All, 1, 2]]], denom*First[bb[[All, 1, 2]]]], 
       If[Max[m] > 0, -denom, denom], 
       If[Max[m] > 0, "", "+"]],
     StringForm["General Form: `1`x`4``3`y`5``2`=0", 
       If[Max[m] < 0, -denom*First[m], denom*First[m]], 
       -If[Max[m] > 0, -denom*First[bb[[All, 1, 2]]], denom*First[bb[[All, 1, 2]]]],
       If[Max[m] > 0, -denom, denom], 
       If[Max[m] > 0, "", "+"], 
       If[If[Max[m] < 0, -denom*First[m], denom*First[m]] > 0, "+", ""]],
     StringForm["Point Slope Form: y`4``1`=`2`(x`5``3`)", 
       Abs[Max[Drop[p1, 1]]], 
       Max[m], 
       Abs[Max[Take[p1, 1]]], 
       If[Max[Drop[p1, 1]] >= 0, "-", "+"], 
       If[Max[Take[p1, 1]] >= 0, "-", "+"]]}])

You won't be able to hide the code, but you can ignore it pretty easily and run cases like this:

p1 = {-5, 4};
m0 = 9/8;
resuts

out_1

p1 = {-5, 5};
m0 = 7/8;
resuts

out_2

Notes

  1. You could put the results code in one notebook and the do computations in a 2nd notebook. It is even possible for the 2nd notebook to load the 1st notebook automatically when it is opened.

  2. This isn't best Mathematica practice, but there is nothing really wrong with it. It is pretty much a minimal revision of you current code. It should satisfy your needs.

  3. Modifying your code to bring it up to best practice levels would require a much greater effort and, unless you are going to use the code in an industrial level application, I don't think it worth your effort to carry out that level of revision. Later on, when you have more Mathematica experience best, or at least better, practice methods will become second nature.

$\endgroup$

Your Answer

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

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