3
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ Thanks @Szabolcs for your comment, I fixed my question to refer to @ as Prefix operator (my mistake). ... Thanks for your answer, it is sad to hear there isn't such a thing. $\endgroup$
    – Bichoy
    Commented May 25, 2015 at 20:29
  • $\begingroup$ No, there isn't, because @ has a very high precedence. The issue is with precedences. Should x OP1 y OP2 z be interpreted as (x OP1 y) OP2 z or x OP1 (y OP2 z)? In Mathematica it's the latter only if OP2 has a higher precedence than OP1. $\endgroup$
    – Szabolcs
    Commented May 25, 2015 at 20:30
  • 2
    $\begingroup$ Instead of f @ (g @@ x) you might want to use f @ 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$
    – Szabolcs
    Commented May 25, 2015 at 20:33
  • $\begingroup$ Thanks @Szabolcs for the explanation, indeed f @ Apply[g] @ x does the trick. Thanks for your help. $\endgroup$
    – Bichoy
    Commented May 25, 2015 at 20:38
  • $\begingroup$ Using only prefix operators: Grid@Flatten[#, 1] &@{{#}, # /. lst1} &@{a, b} $\endgroup$
    – Bob Hanlon
    Commented Jun 4, 2015 at 17:10

3 Answers 3

4
$\begingroup$

Instead of f @ (g @@ x) you can use f @ Apply[g] @ x (since Mathematica 10). This is more verbose, but it saves parentheses. Since visually matching parentheses is hard, I think that the latter variant is more readable, especially when there's a long chain of functions.

This style can be used with postfix too (x // Apply[g] // f) and it makes it easy to add or remove functions at any position in the chain without having to worry about precedences.

Most important operators, such as Map, Apply, Replace, etc. support this style.

$\endgroup$
4
  • 1
    $\begingroup$ Similarly f @* g @@ x works, too. $\endgroup$
    – Michael E2
    Commented May 25, 2015 at 20:54
  • $\begingroup$ @MichaelE2 Can you elaborate more on that? What does @* mean in this case? (I like this more ... ) $\endgroup$
    – Bichoy
    Commented May 25, 2015 at 21:00
  • 1
    $\begingroup$ @Bichoy It is short for Composition. You can search this answer for @* and other such forms. $\endgroup$
    – Michael E2
    Commented May 25, 2015 at 22:51
  • $\begingroup$ Thanks @MichaelE2, this is indeed a marvelous answer :) Thanks a lot for your help. $\endgroup$
    – Bichoy
    Commented May 25, 2015 at 23:00
2
$\begingroup$

As suggested by @MichaelE2 in the comments, I ended up using the composition operator @*. My final code looks like:

Grid @* Join @@ {Keys@Take[#, 1], Values@#}& @ lst1
$\endgroup$
0
$\begingroup$
Grid[Join[
  Transpose[Union /@ GatherBy[Flatten[lst1], First][[All, All, 1]]],
  Transpose[Union /@ GatherBy[Flatten[lst1], First][[All, All, 2]]]]]

which of course can be streamlined...

$\endgroup$
1
  • $\begingroup$ Sorry David, I am mostly asking about the Prefix operator @ and evaluation precedence. I am sorry if my question in its current form isn't clear enough. $\endgroup$
    – Bichoy
    Commented May 25, 2015 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.