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: Orderless attribute for named functions and function arguments

  • To: mathgroup at smc.vnet.net
  • Subject: [mg72543] Re: Orderless attribute for named functions and function arguments
  • From: albert <awnl at arcor.de>
  • Date: Sat, 6 Jan 2007 03:34:29 -0500 (EST)
  • References: <enip11$hf9$1@smc.vnet.net>

Hi Hannes,

> Now I want an additional dependence of the result on a name of the
> function in the following form:
> 
> f[functionName_String][
>    "Named argument 1" -> x_,
>    "Named argument 2" -> y_,
>    "Named argument 3" -> z_]:=
> Module[{xx, yy, zz},
>   {xx, yy, zz} = {x, y, z};
>   (*Do something with xx, yy, zz*)
>    ...;
>    (*Return the result depending on functionName*)
>    Which[
>      MatchQ[functionName, "func1"],  value1,
>      MatchQ[functionName, "func2"],  value2,
>      ...]];
> 
> However, it is not possible anymore to assign the attribute Orderless
> as
> 
> SetAttributes[f[functionName_String],Orderless];
> 
> generates an error message:
> 
> SetAttributes::sym: Argument f[functionName_String] at position 1 is
> expected \
> to be a symbol.

As others might have noted this is because Attributes can only be given to
symbols, but f[functionName_String] is an expression.

One thing I just wanted to mention is the following, which basically gives
the same result and is, I think, the usual way to approach what you want.
You can learn more about it when reading about functions with options. It
uses the rules for the named arguments as replacement rules and should work
well even with your prefered notation:

f[functionName_String][rules : (Rule[_String, _] ..)] := Module[{
      argnames = {
         "Named argument 1", "Named argument 2", "Named argument 3"
      },
      xx, yy, zz
      },
      {xx, yy, zz} = Thread[ReplaceAll[argnames, {rules}]];

      (* now use variables as needed *)

      Switch[functionName,
        "func1", value1,
        "func2", value2,
         _,$Failed
      ]
    ]

Maybe you know about this and have special needs which make this approach
unfavourable, I just wanted to mention it in case you are not aware of it.
Of course there are some differences in behaviour, as you would need
additional code to make sure that all arguments are present and none are
present that should not be there. Also I have no idea which of the
approaches is more efficient, but for most use cases the differences should
not matter much, I think.

hth,

albert


  • Prev by Date: Re: A pattern matching problem
  • Next by Date: Re: A pattern matching problem
  • Previous by thread: Re: Orderless attribute for named functions and function arguments
  • Next by thread: Re: [TS 270]--Re:Re: [TS 48]--Re:why isn't Rational[1,2] (apparently) atomic until it is evaluated?