13
$\begingroup$

For example, using newlines after // results in some really weird indentation:

enter image description here

Ideally, I'd love to be able to do something like this:

x //
f1 //
f2 //
f3 //
f4 //
f5

or if possible, this would be even better:

x
// f1
// f2
// f3
// f4
// f5
$\endgroup$
  • 7
    $\begingroup$ The last one is not possible because x on a line by itself is already a complete expression. x// is not a complete expression, so Mathematica will keep reading more from the next time. Similarly, x, newline, +y is two different expressions and not equivalent to x+, newline, y, i.e. x+y. If a line ends with a backslash, Mathematica will keep reading on the next line, but make sure no spaces follow the backslash. This is just a comment, I don't know the answer to your main question. You can use Code cells which don't indent at all but I don't think that's what you want. $\endgroup$ – Szabolcs Apr 17 '15 at 17:05
  • $\begingroup$ I cannot recall seeing anything that would let us modify this indentation behavior but I like the question. $\endgroup$ – Mr.Wizard May 6 '15 at 6:21
  • 2
    $\begingroup$ x // RightComposition[ f1, f2, f3, f4, f5] can have a relatively good appearance. x // f1 /* f2 /* f3 /* f4 /* f5 unfortunately doesn't. $\endgroup$ – Karsten 7. Jul 5 '15 at 10:39
  • 1
    $\begingroup$ This would make a lot of sense considering all the operator forms introduced in v10. The current indentation behaviour for // is quite useless. Have you suggested this to Wolfram? $\endgroup$ – Szabolcs Jul 6 '15 at 12:34
4
$\begingroup$

You can use InputAutoReplacements with a TemplateBox to achieve the desired behavior:

CurrentValue[EvaluationNotebook[], {InputAutoReplacements,"//"}] = TemplateBox[
    {},
    "Postfix",
    DisplayFunction->("//"&),
    InterpretationFunction->("//"&),
    SyntaxForm->"\[VerticalSeparator]"
];

The key is to change the SyntaxForm to a symbol that doesn't indent in the undesired fashion, which is most operators. However, it is necessary to use a symbol whose parsing precedence is lower than the default parsing precedence of "//".

Here's a portion of the precedence table from the documentation:

enter image description here

The above explains why I chose \[VerticalSeparator]for the SyntaxForm setting. Now, using // produces the following (as an image so you can see it in action):

enter image description here

$\endgroup$
2
$\begingroup$

A rough approximation using a string

toExpr[str_String] := 
  str //
    StringReplace[#, "\n" :> ""] & //
   ToExpression;

"
 x
 //f1
 //f2
 //f3
 //f4
 //f5
 " //
 toExpr

f5[f4[f3[f2[f1[x]]]]]

Although, this is just a Composition

Composition[f5, f4, f3, f2, f1]@x

f5[f4[f3[f2[f1[x]]]]]

f5@*f4@*f3@*f2@*f1@x

f5[f4[f3[f2[f1[x]]]]]

% == %% == %%%

True

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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