Is there a neat way to define a function with a single optional argument that defaults to an empty sequence? For example, suppose I want to define a wrapper for RandomReal
that allows zero or one arguments:
myRandom[] := RandomReal[];
myRandom[x_] := RandomReal[x];
I can see two more compact ways of doing this. The first is to allow any number of arguments and then check whether we have zero or one:
myRandom[x___] := RandomReal[x] /; Length[{x}] ≤ 1;
and the other is to use a default argument of Sequence[]
, wrapped with Hold
:
myRandom[x_:Hold[Sequence[]]] := ReleaseHold[RandomReal[x]];
But neither of these seems very neat, and might be problematic or inefficient in some (more realistic) cases. Is there a conventional and/or neat way of doing this sort of thing?
myRandom[x___] := RandomReal[x]
? $\endgroup$myRandom[1, 2, 3]
to pass through. $\endgroup$