The @ operator in Mathematica is right-associative by default, i.e.
f@g@h
evaluates as
f@(g@h)
Is it possible to make the @ operator evaluate the involved functions in a left-associative order (f@g)@h instead?
|
The @ operator in Mathematica is right-associative by default, i.e.
evaluates as
Is it possible to make the @ operator evaluate the involved functions in a left-associative order |
|||
|
A similar question has been addressed here: How can one define an infix operator with an arbitrary unicode character? I strongly encourage you to add an operator, if necessary, rather than changing the behavior of the built-in one. Nevertheless, if you are undeterred might be able to make this change in a similar fashion but I expect it to break the system if you are successful. |
|||||||||||||
|
|
The problem is that the conversion happens at parsing stage, not evaluation. And after code has been parsed, the details of how function was invoked (prefix, normal way or postfix) are not stored any more, so by the time you evaluate the code, you have no way to tell whether you typed it as In the FrontEnd, you can do something like this:
followed by
Then,
Note that the above code may not be totally robust, since it is based on interpreting box expressions, and I may have missed some possibilities. It should be possible to make it reasonably robust though, by extending to more cases. Since a single |
|||
|
|
obj@method1[args]@method2[args2]and it will evaluate correctly from left to right. I want to mimic that method invocation syntax. – sakra Oct 11 '12 at 13:38//? – sebhofer Oct 11 '12 at 14:06method1[obj, args]). And if they wanted to have objects to the left, provide a general syntax (e.g. an extended infix syntax, transforminga~b~[...]intob[a,...], so you could writeobj~method~[args], but also e.g.5~IntegerDigits~[2,3]). – celtschk Oct 11 '12 at 16:37~infix~has been a hard sell. :^) – Mr.Wizard♦ Oct 11 '12 at 16:50