8

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]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Red Banana
  • 5,329
  • 2
  • 29
  • 47

5 Answers5

17

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.

Brett Champion
  • 20,779
  • 2
  • 64
  • 121
15

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[addArgs, HoldAll]

addArgs[func_, args___] := Function[, func[#, args], HoldFirst]

Table[8^m - 1, {m, 1, 20}] // addArgs[TableForm, TableAlignments -> Right]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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 08:46
9

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}
Rojo
  • 42,601
  • 7
  • 96
  • 188
4

Since it's options you need to modify, why don't you just use SetOptions to do what you need? This will make the default table alignment to the right

SetOptions[TableForm,TableAlignments -> Right]

Then just use TableForm in any form you like. It will have the desired option set.

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

or

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

Or even

TableForm[Table[8^m - 1, {m, 1, 20}]]
Peltio
  • 5,516
  • 3
  • 27
  • 27
2

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] &
Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96