Message Boards Message Boards

GROUPS:

[feature req] GUI-item for showing parentheses of grouping

Posted 26 days ago
577 Views
|
8 Replies
|
14 Total Likes
|

When we write code and don't use parentheses, Mathematica still knows the grouping of expressions and operators, which results in a determined order of execution, because of the system's internal set of rules for priority/precendence. We can and sometimes must control the grouping of expressions/operators by adding parentheses in order to get the desired output. If parentheses aren't needed, we can see in FullForm[] that they get dropped along the way by Mathematica.

My idea/suggestion/feature request (for us to discuss, and for the Wolfram developers to consider) is:

How about a GUI-item, maybe in form of a button or within a context menu, which automatically expands the selected code in-line by adding parentheses for the recognized code structure, thus making the implicit precedence and grouping visible?

When we "multi-click" (double-click, triple-click, quadruple-click, quintuple-click, etc) with the left mouse-button in the middle of any large chunk of code, Mathematica automatically recognizes and selects the next bigger balanced compound expression ("code snippet"): this visual auto-select feature is already helpful in giving us a rough idea about the code line structure from inner to outer expressions. The context menu item "Un/Comment" alters the code by automatically adding $\text{(* *)}$ to the selected code. So why not have a similar context menu item for the auto-selected code snippet called "Show Grouping" or "Explicitize Grouping"?

My idea is powerful. I might be a beginner and always stay at that level but my ideas are great and contribute to the improvement of the Mathematica product, if they get implemented. I would love to hear what the Wolfram developers think of my idea and what the odds are that it gets implemented the sooner the better, thanks.

8 Replies

Just so I understand, for something like this:

f /@ g /@ {1,2,3}

You want a keystroke or menu item which adds parentheses, e.g. like so:

f /@ ( g /@ {1,2,3} )

To make the grouping more clear?

Yes Sir ! Loving it . That's a primitive yet still good example , thank you . A more involved "input" for the new menu item to process would be:

NestWhile[
  Join @@ Table[
     Table[ReplacePart[s, #1 -> n], {n, #2}] & @@ 
      First@SortBy[{#, 
           Complement[Range@9, s[[First@#]], s[[;; , Last@#]], 
            Catenate@
             Extract[Partition[s, {3, 3}], Quotient[#, 3, -2]]]} & /@ 
         Position[s, 0, {2}], 
        Length@Last@# &], {s, #}] &, {sudoku}, ! FreeQ[#, 0] &]

That is a nesting of five pure functions. The most basic (and trivial) example would be an algebraic code line:

a + b / c * d ^ e

Selecting this code snippet with my mouse, then right-click context menu item "Show Grouping" should transform/expand the selected code snippet in situ ("in-line") to:

a + ((b / c) * (d ^ e)) (*smart transformation*)
(a + ((b / c) * (d ^ e))) (*brute silly transformation*)

Thus making the code snippet more intelligible and debuggable to the reader, if the code is hard to review or doesn't deliver the desired output. Using FullForm[] for understanding in-line code or for debugging purposes is not always the way to go, imho.

Posted 25 days ago

Raspi, the feature you request may, in a way, already exist. It is the keyboard shortcut Ctrl + . (That last charcter is a period)

Documentation

enter image description here

enter image description here

enter image description here

enter image description here

Hi Hans, thanks for participating in the discussion. I am fully aware of that functionality: we can leap around with the mouse pointer and then click around (or use your keyboard shortcut) to get an idea of 'the next bigger balanced compound expression'. Even in your example there's quite some clicking (and memorization) involved and once the selection is gone, the information/clarity about the grouping is lost.

My ultrashort algebraic example wasn't a good example anyway. Better examples involve a combination of pure functions, patterns, Boolean operators, conditionals, summation/integration symbols, rules, and alike; there we learn that we must place parentheses! Usually for conciseness and elegance we do not write down superfluous parentheses or we simply forget to write them, when they're needed. Seeing parentheses, even if they are indeed superfluous, can help intelligibility and provides unambiguousness to the reader.

So, your "Ctrl + ." comes first, yes, and for some users/uses it might be enough. It selects a balanced expression and the selection expands to compound expressions depending on the position of the mouse pointer (poor mouse pointer placement shown hehe):

enter image description here

Then my "Show Groupings" comes next, it makes all (or all relevant) groupings within that pre-selected compound expression readily visible to any reader, not just to the person with the keyboard/mouse ;-)

Mind you, on a print-out (paper, book, screenshot) one cannot click around anymore. In such a case, unambiguous code like a+((b/c)*(d^e)) proves to be a good alternative to the reader/debugging user. In summary, "your" and "my" features go well together/belong together/complement each other, yes, but in terms of practical usage they are different enough.

Hi Raspi,

not really what you want, but here is at least a simple function for examining expressions:

Attributes[makeObvious] = HoldAll;
(* makeObvious[expr_]:=Framed//@HoldForm[expr] *)
makeObvious[expr_] := Map[Framed, HoldForm[expr], {2, Depth[expr] - 1}]

With this we get e.g.:

enter image description here

Hi Henrik, thank you! Very cool, seems to work well with (smaller?) expressions (e.g. not with the NestWhile[] expression from my earlier post).

Imho it should be feasible to implement the idea, if the Wolfram GUI team likes it. Hope you'd like it!

Posted 24 days ago

Hi Raspi,

Thank you for your suggestion. I would just like to confirm to you that it has been forwarded to the appropriate developers.

In order to make our technical teams aware of your ideas for improvements, please use our Online Feedback Form so they can learn about it without delay.

Thank you very much!

Thanks for thanking, the prospect (let me have some hopes up for this proposed new feature - your team might decide against it eventually, haha) and your attention @Peter, @Arnoud, @Karl, appreciated ymmd!

Crossing fingers, I would really enjoy and profit from such a feature in the GUI of Mathematica! And not only me, I am sure ::))

I will use your linked Product Feedback Form in future, too. Good to know, thank you.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract