Skip to main content
5 of 6
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/

Why doesn't a Composition[] hold its arguments and what can be done about it?

Now that Composition (also RightComposition) has shorthand syntax I am more inclined to use it, however what I feel should be one of its best advantages over the plain foo @ bar @ ## & doesn't exist: held arguments. For example I wish for the following behavior:

Composition[ToString, HoldForm][2 + 2] (* desired output: "2 + 2" *) 

I am aware that the form head[a__][b__] doesn't normally allow holding of arguments b but this has always seemed like a place an exception should have been made. Unless the first head applied has hold Attributes this would not alter behavior. Does anyone regularly make use of the evaluation of arguments that I am seeing as undesirable?

Presumptuously assuming that others find desirable the behavior that I want I see this as a sorely missed opportunity:

  • the shorthand forms foo @* bar and foo /* bar could have been made to parse as LeftComposition (hypothetical) and RightComposition
  • these new operators could have been made to hold arguments as I describe without breaking backwards-compatibility with Composition in past versions.

I wonder what can now be done to improve the situation.

  1. I suppose it is too late to get such changes made to natively. Has such a change ever been made to Mathematica (or Wolfram Langauge) after an initial release?

  2. I am tempted to try to force this behavior myself using one of the methods available. All public code except what I see as edge cases would work on my machine but my own code would fail on others.

  3. Sadly it is not possible to define custom compound operators so there is no true alternative, but I could define e.g. comp[fn1, fn2] to work as I desire.

My questions therefore:

Is there some reason not to have a Composition expression hold its arguments?

Is there another option that I am failing to consider?

How can one robustly implement option #2 or #3?

Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k