Re: OptionsPattern[...]
- To: mathgroup at smc.vnet.net
- Subject: [mg118854] Re: OptionsPattern[...]
- From: ADL <alberto.dilullo at tiscali.it>
- Date: Fri, 13 May 2011 06:27:09 -0400 (EDT)
- References: <iqdh9c$19b$1@smc.vnet.net>
On 11 Mag, 10:27, "Scot T. Martin" <smar... at seas.harvard.edu> wrote:
> This is a question about elegance.
>
> For the setups below for funcOne and funcAlpha, you will see the difference in the use of Sequence[...].
>
> funcOne can potentially pass "{ }" to funcTwo, which is undesirable for funcTwo. That is, a call of funcOne[value] without options will pass "{ }" to funcTwo.
>
> For funcAlpha[value], an empty pattern will be passed instead of "{ }" to funcBeta but requires the non-elegant formulation of Sequence[...]. This for mulation has the advantage of passing cascading options from one function to the next without progressively deeper "{ }", i.e., becoming "{{ }}" on next nested function call.
>
> Does anyone know how to use options better (i.e., in a more elegant fashion) than represented in my examples, i.e., to get the functionality of funcAlpha without the use of Sequence[..]?
>
> funcOne[x:_, opts:OptionsPattern[]]:=
> With[{ appropriateOptions = FilterRules[{opts},Options[funcTwo]] },
> funcTwo[x,appropriateOptions]]
>
> funcAlpha[x:_, opts:OptionsPattern[]]:=
> With[{ appropriateOptions = Sequence@@FilterRules[{opts},Options[funcBeta]] },
> funcBeta[x,appropriateOptions]]
If I understand correctly, the desired behavior would be to write the
function in the following way:
ClearAll[funcAlphaWished];
funcAlphaWished[x__,opts:OptionsPattern[]]:=
With[{appropriateOptions=FilterRules[opts,Options[funcBeta]]},
funcBeta[x,appropriateOptions]
];
Now this is not possible since FilterRules does not accept more than
two arguments.
As far as I understand, the following logic should be built-in:
ClearAll[filterRules];
filterRules[x_,y__,p_] := Sequence@@FilterRules[Join[{x},{y}],p];
filterRules[x_List, p_] := FilterRules[x, p];
filterRules[x_, p_] := Sequence@@FilterRules[{x},p];
With this modification, the wished form seems to work (of course using
the new filterRules):
ClearAll[funcAlphaWished];
funcAlphaWished[x__,opts:OptionsPattern[]]:=
With[{appropriateOptions=filterRules[opts,Options[funcBeta]]},
funcBeta[x, appropriateOptions]
];
Probably, issues may come with a single option, since now the behavior
is different:
In[]:= FilterRules[AlignmentPoint -> Center, Options[Plot]]
Out[]= {AlignmentPoint -> Center}
In[]:= filterRules[AlignmentPoint -> Center, Options[Plot]]
Out[]= Sequence[AlignmentPoint -> Center]
ADL