MathGroup Archive 2009

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

Search the Archive

Re: Passing function arguments as lists of replacement

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104683] Re: [mg104661] Passing function arguments as lists of replacement
  • From: Leo Alekseyev <dnquark at gmail.com>
  • Date: Sat, 7 Nov 2009 06:45:06 -0500 (EST)
  • References: <3794357.1257503749087.JavaMail.root@n11>

This is certainly one option, but does it have any benefits over the
list of string replacement rules?..

I recently started using both the options pattern _and_ a replacement
list.  The actual functions I'm working with look something like

foo[x_,y_,parameters_,OptionsPattern[]],

where x and y are bona fide dependent variables, parameters is a list
of fixed constants pertinent to the system (provided as replacement
rules of the form, e.g. {"a"->1,...}, and OptionsPattern[] conveys
certain options pertaining to the computation itself, e.g.
{AvoidDivideByZero->True}

So far it seems like a sensible approach, but I am still wondering if
other people do similar things in their code.

--Leo

On Fri, Nov 6, 2009 at 8:57 AM, David Park <djmpark at comcast.net> wrote:
> 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
>
>
>


  • Prev by Date: Re: How to make floating control elements?
  • Next by Date: Re: Monitoring using Mathematica
  • Previous by thread: Re: Function returns null
  • Next by thread: Re: Passing function arguments as lists of replacement rules