Postfix with two arguments
Let me start with the second question first as it is more direct. You can see for yourself how these inputs work:
f @ x
x ~f~ y
x // f
f[x] f[x, y] f[x]
The first and third only work with a single argument. Similar to but distinct from the first is @@
which is shorthand for Apply
, and it allows:
f @@ {x, y}
f[x, y]
Here the Head List
is replaced with f
. For more clarity see: Scan vs. Map vs. Apply
With the single-argument forms you can use Sequence
to insert multiple arguments, but only if the function (f
) does not have the Attribute SequenceHold
or HoldAllComplete
:
f @ Sequence[x, y]
Sequence[x, y] // f
f[x, y] f[x, y]
And an example of where this will not work:
Rule @ Sequence[x, y]
Rule::argr: Rule called with 1 argument; 2 arguments are expected. >>
Rule[Sequence[x, y]]
If you wish to use a multi-argument function with //
application you can make it a parametrized, SubValues, or operator form function, all meaning essentially the same thing. See:
- Define parameterized function
- V10's Operator Forms - what are they good for?
A very simple example:
f[a__][x_] := g[a, x]
Now:
y // f[x]
g[x, y]
x // f[1, 2, 3]
g[1, 2, 3, x]
See the first bulleted link above for more examples and theory.
Related:
Is it possible to insert arguments into functions when they're used like Function@ or //Function?
Syntax for prefix mode with multiple arguments using @ shorthand
(Just answering the first question. Others have answered the second question thoroughly.)
It's worth mentioning that you can do
{x,y} // (f @@ #) &
f[x,y]
(No parentheses needed, but I added them for clarity.)
You could even make your own function to do this (slightly) more compactly, especially if you have trouble remembering the @
's and #
's :
ClearAll[pf]
pf[ argList_, f_ ] := f @@ argList
Then you can do what you want using an infix operator:
{x,y} ~pf~ f
f[x,y]
Edit:
Lately I've taken to writing my first example above as
{x,y} // Apply[f]
or
{x,y} // Apply@f
which I fine somewhat more readable, particularly when there are other pure functions involved. Note that this uses the operator form of Apply
.
According to my basic understand of these notations:
For infix notation, the function must be a binary operator, e.g.
g[x_, y_] := x^2 - y^2;
Then
5~g~2
Gives
25-4=21
For postfix notation, the function must take one argument
f[{x_,y_}]:=x^2-y^2
So
{5,2}//f
gives 21