2

Is there a way to get an optional arguments to work in an anonymous function.

Function[{Optional@a},If[a,True,False]]
William
  • 7,595
  • 2
  • 22
  • 70
  • 6
    Related http://mathematica.stackexchange.com/questions/29206/is-there-any-way-to-define-pure-functions-with-optional-arguments – Basheer Algohi Nov 01 '16 at 02:28

2 Answers2

2

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.

Jacob Akkerboom
  • 12,215
  • 45
  • 79
1

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]
William
  • 7,595
  • 2
  • 22
  • 70