Is it possible to insert arguments into functions when they're used like Function@ or //Function?

Is it possible to insert arguments into functions when they're used like Function@ or //Function? Sometimes I need to do this, but then I have to return to the standard function usage: Function[]

Imagine I have this:

Table[8^m - 1, {m, 1, 20}]//TableForm


Or this:

TableForm@Table[8^m - 1, {m, 1, 20}]


But I don't like the default alignment and I want to use another alignment, say right. Will I always have to transform the above function into this:

TableForm[Table[8^m - 1, {m, 1, 20}], TableAlignments -> Right]

-
@Rojo I gave an example. – Gustavo Bandeira Jun 18 '12 at 3:05
I keep reading the 8^m as an emoticon, and wonder what it's doing inside a Table. :-P – Brett Champion Jun 18 '12 at 3:59
@BrettChampion I was solving an exercise from this book: What number is expressed in octal by m consecutive sevens? The answer is 8^m-1. I was just testing it. – Gustavo Bandeira Jun 18 '12 at 4:37
In a nutshell: yes, but it looks messy. – 0x4A4D Jun 18 '12 at 7:30
@BrettChampion: It's an emotional table. :-) – celtschk Jun 18 '12 at 8:41

4 Answers

Perhaps you're looking for something like this:

args = {1, 2, 3};

func @@ args

args // func@@#&


Or even:

args /. {x__} :> func[x]


Since it appears Brett better understood your original question I'll try to save this answer by giving a variation of the last one above:

Table[8^m - 1, {m, 1, 20}] /. x_ :> TableForm[x, TableAlignments -> Right]


Of course this is probably better:

Table[8^m - 1, {m, 1, 20}] // (TableForm[#, TableAlignments -> Right] &)


Taking this in a different direction, if you have a function like TableForm that you often want to use in this fashion, let me suggest an alternative:

myTable[opts___][tab_] := TableForm[tab, opts]


Now:

myTable[TableAlignments -> Right] @ Table[8^m - 1, {m, 1, 20}]


or:

Table[8^m - 1, {m, 1, 20}] // myTable[TableAlignments -> Right]


This approach could also be used for generic functions but the syntax may become unwieldy:

SetAttributes[addOpts, HoldAll]

addOpts[func_, opts___] := Function[, func[#, opts], HoldFirst]

Table[8^m - 1, {m, 1, 20}] // addOpts[TableForm, TableAlignments -> Right]

-
 I'm expecting an edit with an infix solution – Rojo Jun 18 '12 at 3:06 @Rojo looks like I'll disappoint. – Mr.Wizard♦ Jun 18 '12 at 3:08 Of course the last one could be written as Table[8^m - 1, {m, 1, 20}] // TableForm ~addOpts~ (TableAlignments -> Right) – celtschk Jun 18 '12 at 8:46 Of course Table[...] ~TableForm~ (TableAlignments -> Right) is simpler in this case. However one could imagine to make addOpts take a list as second argument which gets interpolated, so you could then write Table[...] // TableForm ~addOpts~ {TableAlignments -> Right, TableHeadings -> {whatever}}. BTW, it need not be options which are added (so addOpts is perhaps a bad name for that function), but it could also be used as expression // Simplify ~addOpts~ conditions. – celtschk Jun 18 '12 at 8:54 @celtschk I think that would be an abuse of ~infix~ notation myself. Imagine that. :-) Though I agree addArgs might be better. – Mr.Wizard♦ Jun 18 '12 at 16:36

Some possibilities:

In[15]:= a // f[#, 2] &
Out[15]= f[a, 2]

In[16]:= f[#, 2]& @ a
Out[16]= f[a, 2]

In[17]:= #~f~2& @ a
Out[17]= f[a, 2]

In[18]:= a // #~f~2 &
Out[18]= f[a, 2]


although you tend to lose legibility.

-

If you go for infix, just be careful with the parentheses

Table[8^m - 1, {m, 1, 20}]~TableForm~(TableAlignments -> Right)


In these particular cases, it's good to remember that options can also be input as lists, so you could also do

Table[8^m - 1, {m, 1, 20}]~TableForm~{TableAlignments -> Right, TableDirections -> Row}

-
Making papa proud. ;^) – Mr.Wizard Jun 18 '12 at 3:18
@Mr.Wizard, father's day in here ;) – Rojo Jun 18 '12 at 3:18
Damn! While reading the answer I was sure I recognized the author. – belisarius Jun 18 '12 at 3:45

Since it hasn't been explicitly stated yet I'll add also this variant, which just defines a custom TableForm which then will enable one to use it in the convenient postfix form. Namely,

Clear[myTab];
myTab[table_List] := TableForm[table, TableAlignments -> Right];
Table[8^m - 1, {m, 1, 20}] // myTab


gives you the required structure. The same with #& notation:

Table[8^m - 1, {m, 1, 20}] // TableForm[#, TableAlignments -> Right] &

-