Is there a way to get an optional arguments to work in an anonymous function.
Function[{Optional@a},If[a,True,False]]
Is there a way to get an optional arguments to work in an anonymous function.
Function[{Optional@a},If[a,True,False]]
Considering the other Q&A is much more likely to be found and the fact that my answer fits there quite nicely, I have moved my answer there.
This appears to work.
optionalFu =
Function[Null,
ReplacePart[
Function @@ Hold[
Null,
Quiet[## &, {Function::slotn}],
HoldAll
]
,
{2, 1, 0} -> Function @@ Hold[##]
], HoldAll]
and an example
optionalFu[If[ToString@#1 == "#1", True, False]][1]