Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: Defining a Function with an Indeterminate Number of Arguments

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81273] Re: Defining a Function with an Indeterminate Number of Arguments
  • From: "David Park" <djmpark at comcast.net>
  • Date: Tue, 18 Sep 2007 00:40:23 -0400 (EDT)
  • References: <fclbai$fh8$1@smc.vnet.net>

Looking at a simpler example:

Options[DonFunction] = {DonColor -> Black};
SyntaxInformation[
   DonFunction] = {"ArgumentsPattern" -> {__, OptionsPattern[]}};
DonFunction[args__, OptionsPattern[]] :=
 Module[{color},
  color = OptionValue[DonColor];
  Style[#, FontColor -> color] & /@ {args}
  ]

DonFunction[a, b, c]

DonFunction[a, b, c, d, e, DonColor -> Red]

And note specifically that the double blank, __, should be used to indicate 
1 or more arguments and not the double dot.

-- 
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/


"Donald DuBois" <donabc at comcast.net> wrote in message 
news:fclbai$fh8$1 at smc.vnet.net...
> Hello -
>
> I am trying to define a function with the following two properties :
>
>  (A) The function should be able to take an indeterminate number of
> arguments without using the List structure (like the Which or StringJoin 
> functions in Mathematica).
>
>  (B) The function should have the capability of defining and using options 
> in the usual way  (i.e. using the Options function outside the package 
> definition
> to define the option default values and using OptionValue within the 
> function definition to retrieve the options with locally defined option 
> values overriding the default values).
>
> As a first attempt, fnc1 below is able to have as
> input an indeterminate number of arguments.
>
> fnc1[args : _ ..] := Module[{},
>   values = args;
>
>   Print["Argument Values: "];
>   Table[Print[i, "   ", {values}[[i]], " Head ",
>     Head[{values}[[i]]]], {i, Length[{values}]}];
>   ]; (* End Module *)
>
> fnc1[b, 3, "strg1"]
>
> Argument Values:
>
> 1   b Head Symbol
>
> 2   3 Head Integer
>
> 3   strg1 Head String
>
>
> However, when I try (in fnc2 below) to define default option values in the 
> usual way for this function, I am unable to retrieve them - only the 
> locally defined option values (opt1 in the e.g. below) become known to the 
> function.
>
>
> (* Define Default Options *)
> Options[fnc2] =
>    {
>      opt1 -> 10,
>      opt2 -> 20
>      };
>
> fnc2[args : _ ..] := Module[{},
>
>      (* Extract the non-rule arguments from args *)
>
>      values = Cases[{args}, Except[_Rule]];
>
>      Print["Argument Values: "];
>      Table[
>    Print[i, "   ", values[[i]], " Head ", Head[values[[i]]]], {i,
>     Length[values]}];
>
>      (* Extract the options from args *)
>      localOpts = Cases[{args}, _Rule];
>
>      Print["local options list = ", localOpts];
>
>   Print["Final Option Values: "];
>      Print["opt1 = ",
>        localOpts /. OptionsPattern[] :> OptionValue[opt1]];
>      Print["opt2 = ",
>        localOpts /. OptionsPattern[] :> OptionValue[opt2]];
>
>      ] (* End Module *);
>
> fnc2[b, 3, "strg1", opt1 -> 15]
>
> Argument Values:
>
> 1   b Head Symbol
>
> 2   3 Head Integer
>
> 3   strg1 Head String
>
> local options list = {opt1->15}
>
> Final Option Values:
>
> opt1 = 15
>
> opt2 = opt2
>
>
> There is a messy workaround  (fnc3 below) but the default option values 
> are redefined every time it is used.
>
> fnc3[args : _ ..] := Module[{},
>        defaultOpts = {opt1 -> 10, opt2 -> 20};
>
>      (* Extract the non-rule arguments from args *)
>      values = Cases[{args}, Except[_Rule]];
>
>      Print["Argument Values: "];
>      Table[
>    Print[i, "   ", values[[i]], " Head ", Head[values[[i]]]], {i,
>          Length[values]}];
>
>      (* Extract the local options from args *)
>      localOpts = Cases[{args}, _Rule];
>      Print["local options list = ", localOpts];
>   Print["default options list = ", defaultOpts];
>
>   (* Combine localOpts with defaultOpts where localOpts take
>   precedence *)
>
>   combinedOpts =
>    Flatten[Split[Sort[Join[defaultOpts, localOpts]],
>       First[#1] === First[#2] &] /. {dd_Rule, ddd_Rule} -> ddd ];
>
>   Print["Final Combined Option Values: "];
>      Print["opt1 = ",
>        combinedOpts /. OptionsPattern[] :> OptionValue[opt1]];
>      Print["opt2 = ",
>        combinedOpts /. OptionsPattern[] :> OptionValue[opt2]];
>
>      ]; (* End Module *)
>
> fnc3[b, 3, "strg1", opt1 -> 15]
>
> Argument Values:
>
> 1   b Head Symbol
>
> 2   3 Head Integer
>
> 3   strg1 Head String
>
> local options list = {opt1->15}
>
> default options list = {opt1->10,opt2->20}
>
> Final Combined Option Values:
>
> opt1 = 15
>
> opt2 = 20
>
> Is there a way to define a function that has the two properties, (A and B 
> above) so that it takes an indefinite number of arguments (without the 
> List curly brackets or List function) and where options can be defined 
> using the Options and OptionValue functions in the usual way?
>
> Thank you in advance,
>
> Don
> 



  • Prev by Date: Re: Single-step evaluation in Mathematica
  • Next by Date: Re: Defining a Function with an Indeterminate Number of Arguments
  • Previous by thread: Re: Defining a Function with an Indeterminate Number of Arguments
  • Next by thread: Re: Defining a Function with an Indeterminate Number of Arguments