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
andfoo /* bar
could have been made to parse asLeftComposition
(hypothetical) andRightComposition
- 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.
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?
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.
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?
f @* g @* h @ x
it is not very likely thatf
,g
orh
hold their arguments anyway. On the other hand, people will maybe use the evaluation, wherex
is a variable containing a larger expression which needs to be evaluated. As for your option #1: I'm only aware of theLocked
attribute ofLocked
, where a thing was changed which was already for very long in the core system. $\endgroup$(2+2)
anyway. Isn't it easier to just writeToString@*HoldForm@Unevaluated[2 + 2]
than bending the system? Or maybeToString@*HoldForm@*Plus @@ {2, 2}
in this specific case. I guess reimplementing Composition is not a good idea. Although it works here, in generalComposition
can be used with e.g.InverseFunction
which breaks this approach. $\endgroup$f @* g @ x
is preferred overf @ g @ x
? I can understand that we may need to defineh = f @* g
and later useh @ x
. Is there any other reasons? $\endgroup$b
can't be held inf[a][b]
. It's a property of how evaluation works and to change it would require a significant change to the evaluator, as well as thinking through all the consequences. I could think of several interesting applications other thanComposition
ifb
could be held, e.g. it would be useful withInactive
. $\endgroup$