Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]
share|improve this question
add comment

5 Answers

up vote 12 down vote accepted

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]
share|improve this answer
    
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
add comment

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}]]
share|improve this answer
add comment

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] &
share|improve this answer
add comment

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}
share|improve this answer
    
Making papa proud. ;^) –  Mr.Wizard Jun 18 '12 at 3:18
1  
@Mr.Wizard, father's day in here ;) –  Rojo Jun 18 '12 at 3:18
1  
Damn! While reading the answer I was sure I recognized the author. –  belisarius Jun 18 '12 at 3:45
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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