RE: Passing function arguments as lists of replacement rules
- To: mathgroup at smc.vnet.net
- Subject: [mg104697] RE: [mg104661] Passing function arguments as lists of replacement rules
- From: "David Park" <djmpark at comcast.net>
- Date: Sat, 7 Nov 2009 06:47:46 -0500 (EST)
- References: <3794357.1257503749087.JavaMail.root@n11>
Use the newer Version 6 Options facilities.
ClearAll[foo]
Options[foo] = {a -> 0, b -> 0, c -> 0};
SyntaxInformation[foo] = {"ArgumentsPattern" -> {OptionsPattern[]}};
foo[OptionsPattern[]] :=
Module[
{aval = OptionValue[a],
bval = OptionValue[b],
cval = OptionValue[c]},
{aval, bval, cval}]
foo[a -> 1, b -> 2]
{1, 2, 0}
Then try typing:
foo[a -> 1, q -> 2]
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/
From: dnquark [mailto:dnquark at gmail.com]
I wish to (a) avoid having to pass a dozen parameters to a function
and have to deal with remembering which goes where and (b) retain the
flexibility in terms of adding new parameters. It seems that a good
solution would be to pass my parameters as a structure with named
fields (this is how I do it in another system). In Mathematica, I came up
with something like this:
foo[paramList_] := Module[{a,b,c},
{a,b,c} = {"a","b","c"}/.paramList;
{a,b,c}
]
sample usage: e.g. foo[{"a"->1,"b"->2}]
My question to the group: is this a good solution? Are there better
ways to achieve my goals?..
Thanks,
--Leo