# Is there any way to define pure functions with optional arguments?

For example consider the following function:

Function[{u,v},u^2+v^4]

Is there anyway to define default values for the variables u and v?

I know that this is possible for ordinary functions as follows:

f[u_:1,v_:0]:=u^2+v^4

But I am looking for a way to do this with pure functions defined by Function[].

-

As far as I know there is no way to do this with the named parameter form of Function but you can use destructuring methods with SlotSequence (##):

f = {##} /. {u_: 1, v_: 0} :> body[u, v] &;

f[]
f[7]
f[7, 8]
body[1, 0]

body[7, 0]

body[7, 8]

It is possible to give your pure function Attributes using an undocumented form.
For Hold attributes you could use Hold or HoldComplete:

g = Function[Null, Hold[##] /. _[u_: 1, v_: 0] :> HoldForm[{u, v}], HoldAll];

g[1 + 1]
{1 + 1, 0}
-
I was thinking about the following construct: Function[{u, v}, u^2 + v^4 /. {u -> 1, v -> 1}] it is not the same as with optional arguments bacause one need to put coma: f[,]. Also, it seems to work well with Numeric arguments while for others ("a" for example), precedences are different. – Kuba Jul 26 '13 at 8:16
But if well understood it can be very useful (in case of many agruments) because you can specify which argument you want to take it's optional value. For example f[1,2,,2,,3], what is not so easy with standard optional arguments. – Kuba Jul 26 '13 at 8:23
@Kuba Okay, why don't you post that as an answer? It is an interesting interpretation of the problem. – Mr.Wizard Jul 26 '13 at 8:41
Could you tell something about this Null issue you have faced lately? – Kuba Jul 26 '13 at 9:19

I have left bottom part of this answer as a warning against not thinking :)

Here is my improvement. It is not so handy but it allows us to specify which argument has to take it's optional value.

edit - scoping.

f = Module[{x, y, z},
Function[{u, v, g}, x^2 y^4 + z^4 /. {x -> (u /. Null -> 3),
y -> (v /. Null -> 2),
z -> g}] (*3rd arg has no opt. value*)
]

f[2, 1, 1]
f[, , 1]
f[1, , 1]
5

145

17

I do not know if it is worth the effort but one may want to scope x, y, z and make replacement more compact.

## the warning :)

The following method will work only when the result of the function has different value than the value of one of arguments. I such case the result is will be replaced by it's optinal value.

Following examples are only lucky coincidence

This approach is not identical as standard optional arguments but it is interesting (but not working :) ).

f = Function[{u, v}, u^2 + v^4 /. {u -> 1, v -> 1}]

f[1,2]
17
f[,]
2

As you see, it is not like with standard optional arguments because you have to put comma inside. Thats because

f = Function[{u,v} ...]

-
The precedence here isn't that strange according to the output of f[,] // Trace, this also explains why something like f[a, 4] fails: the main procedure here is Null^2 + Null^4 /. {Null -> 1, Null -> 1}. – xzczd Jul 26 '13 at 8:52
@xzczd My first thouth was about this but then I checked f[1,4] and assumed that 4->1 will make an output 2. This is not happening so I'm confused. – Kuba Jul 26 '13 at 8:56
That's because ReplaceAll first evaluate the expression to be replaced before the replacement(ReplaceAll doesn't have Attributes like HoldFirst or HoldAll), so when the replacement begins, 1^2+4^4 has already evaluated to 257, so 4->1 won't work. – xzczd Jul 26 '13 at 9:04
f[1, 2 a] gives the expected result, (1+16 a^4), but f[1, a] gives (2) which is not the expected result. I don't know why this happens. – M6299 Jul 26 '13 at 9:07
@M6299 That's because when the replacement really begins, the expression has become 256 + 4 a^2 /. {2 a -> 1, 4 -> 1}, 4 a^2 doesn't match 2 a, so it remains as it is :) – xzczd Jul 26 '13 at 9:11