Is there a way to force evaluation on the right before the Prefix operator (@
) without using parentheses? I came across this question when I was working with the following example:
I have a list of substitutions like this:
lst1 = {{a -> 1 , b -> 2}, {a -> 3, b -> 4}, {a -> 5, b -> 6}}
I am trying to put it in a grid form, to look like this:
a b
1 2
3 4
5 6
I get this code working:
Grid @ (Join @@ { Keys @ Take[#, 1], Values @ # } & @ lst1 )
However, it only works with the parenthese ( )
around the Join
expression.
Is there a way in Mathematica to force everything on the right of an @
to get evaluated first?
I wish I can right something like:
Grid @ Join @@ { Keys @ Take[#, 1], Values @ # } & @ lst1
Which instead yields:
Grid[Join][{{a, b}}, {{1, 2}, {3, 4}, {5, 6}}]
The reason I am trying to do that, is basically, when I get to apply many functions in this way, it becomes cumbersome to always have to close the parentheses at the end (which defeats my purpose of using the @
operator in the first place).
I am mostly looking for an equivalent to the $
operator in Haskell, if such thing exists.
@
has a very high precedence. The issue is with precedences. Shouldx OP1 y OP2 z
be interpreted as(x OP1 y) OP2 z
orx OP1 (y OP2 z)
? In Mathematica it's the latter only ifOP2
has a higher precedence thanOP1
. $\endgroup$f @ (g @@ x)
you might want to usef @ Apply[g] @ x
, which is more verbose, but does save parentheses. Unfortunately when you use pure functions (...&
notation), very often it's still necessary to use parens because&
has a low precedence and tends to "eat" everything preceding it. $\endgroup$f @ Apply[g] @ x
does the trick. Thanks for your help. $\endgroup$Grid@Flatten[#, 1] &@{{#}, # /. lst1} &@{a, b}
$\endgroup$