Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Passing function arguments as lists of replacement rules

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104690] Re: Passing function arguments as lists of replacement rules
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Sat, 7 Nov 2009 06:46:25 -0500 (EST)
  • References: <hd0t9j$82h$1@smc.vnet.net>

dnquark schrieb:
> 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?..

It is a good and very common solution. Look at Options and you will find
out that there are even more comfortable ways to achieve what you want
and more, e.g. default values:

Options[foo]:={"a"->1,"b"->2,"c"->3}

foo[opts:OptionsPattern[]]:=Module[{a,b,c},
 {a,b}={"a","b"}/.opts;
 c = OptionValue["c"];
 {a,b,c}
]

foo["a"->3,"c"->7]

hth,

albert


  • Prev by Date: Re: Passing function arguments as lists of replacement rules
  • Next by Date: Re: Can't figure out how to include syntax coloring in Printout
  • Previous by thread: Re: Passing function arguments as lists of replacement rules
  • Next by thread: RE: Passing function arguments as lists of replacement rules