What\'s the best/canonical way to define a function with optional named arguments? To make it concrete, let\'s create a function foo
with named arguments a>
I'll throw this possible solution into the mix:
foo[opts___Rule] := Module[{f},
f@a = 1; (* defaults... *)
f@b = 2;
f@c = 3;
each[a_->v_, {opts}, f@a = v];
Return[bar[f@a, f@b, f@c]]
]
I like it for its terseness but I don't think it's the standard way. Any gotchas with doing it that way?
PS, it uses the following handy utility function:
SetAttributes[each, HoldAll]; (* each[pattern, list, body] *)
each[pat_, lst_, bod_] := (* converts pattern to body for *)
Scan[Replace[#, pat:>bod]&, Evaluate@lst] (* each element of list. *)