Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Optional arguments & FilterRules

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81837] Optional arguments & FilterRules
  • From: Will Robertson <wspr81 at gmail.com>
  • Date: Thu, 4 Oct 2007 04:32:31 -0400 (EDT)

Hello all,

The Mathematica documentation contains an example of how to send
optional arguments to different functions using FilterRules. Something
like this:

Options[g] = {b -> b0};
g[OptionsPattern[]] := {g : OptionValue[b]}

ClearAll[h]
h[x_, opts : OptionsPattern[]] := Module[{},
  {g[Evaluate[FilterRules[{opts}, Options[g]]]], {h : x}}]
h[ccc, b -> bbb]

This is all well and good when the function this is happening inside
doesn't take rule based arguments of its own, but what if it does?

The "obvious" method gives the right result but also sends a warning:

ClearAll[h]
Options[h] = {c -> c0};
h[opts : OptionsPattern[]] := Module[{},
  {g[Evaluate[FilterRules[{opts},
     Options[g]]]], {h : OptionValue[c]}}]

h[b -> bbb]
h[b -> bbb, c -> ccc]

Finally, here's what I managed to cook up to work around the warning:

ClearAll[h];
Options[h] = {c -> c0};
h[opts : OptionsPattern[]] := Module[{Opt},
  gopts = FilterRules[{opts}, Options[g]];
  hopts = FilterRules[{opts}, Options[h]];
  Opt[x_] := OptionValue[h, hopts, x];
  {g[Evaluate[gopts]], {h : Opt[c]}}]

h[b -> bbb]
h[b -> bbb, c -> ccc]

Is there a better way? Perhaps it's better to create a wrapper
function that simply passes its arguments along to the functions doing
the work.

Cheers,
Will



  • Prev by Date: Re: Converting Mathematica into c++ code
  • Next by Date: Re: Interpolating Functions
  • Previous by thread: Re: Manipulate+Plot showing no output
  • Next by thread: Re: [TS 28872]--Re:Pattern::nodef: No default setting found for Piecewise in position 2 when length is 2.