8
$\begingroup$

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]
$\endgroup$
0

5 Answers 5

15
$\begingroup$

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]
$\endgroup$
1
  • $\begingroup$ Of course the last one could be written as Table[8^m - 1, {m, 1, 20}] // TableForm ~addOpts~ (TableAlignments -> Right) $\endgroup$
    – celtschk
    Jun 18, 2012 at 8:46
17
$\begingroup$

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.

$\endgroup$
0
9
$\begingroup$

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}
$\endgroup$
4
  • 1
    $\begingroup$ Making papa proud. ;^) $\endgroup$
    – Mr.Wizard
    Jun 18, 2012 at 3:18
  • 1
    $\begingroup$ @Mr.Wizard, father's day in here ;) $\endgroup$
    – Rojo
    Jun 18, 2012 at 3:18
  • 3
    $\begingroup$ Damn! While reading the answer I was sure I recognized the author. $\endgroup$ Jun 18, 2012 at 3:45
  • $\begingroup$ Joy! SoftmaxLayer[][{1, 3, 2}]~NumberForm~2 $\endgroup$ Apr 30, 2021 at 12:06
4
$\begingroup$

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}]]
$\endgroup$
2
$\begingroup$

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] &
$\endgroup$

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.