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: [mg72528] Re: [mg72525] Orderless attribute for named functions and function arguments
  • From: "Chris Chiasson" <chris at chiasson.name>
  • Date: Fri, 5 Jan 2007 01:46:05 -0500 (EST)
  • References: <200701041204.HAA18347@smc.vnet.net>

since the heads of expressions are evaluated first (except when the
expression is held, obviously), you could just make f[whatever]
evaluate to a symbol and then make that symbol have an orderless
attribute

On 1/4/07, Hannes Kessler <HannesKessler at hushmail.com> wrote:
> Dear experts,
>
> a way to use the attibute Orderless that works for functions with named
> arguments in the form of rules is
>
> SetAttributes[f,Orderless];
> f[
>    "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 and return the result*)
>    ...];
>
> Calling the function with arguments in different ways gives identical
> results.
> No need to care about the order of the named arguments:
>
> f1 = f[
>    "Named argument 1" -> 1,
>    "Named argument 2" -> 2,
>    "Named argument 3" -> 3];
> f2 = f[
>    "Named argument 2" -> 2,
>    "Named argument 1" -> 1,
>    "Named argument 3" -> 3];
> f1 == f2
> --> True
>
> 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.
>
> A simple solution is to reverse the definition of f:
>
> SetAttributes[f,Orderless];
> f[
>    "Named argument 1" -> x_,
>    "Named argument 2" -> y_,
>    "Named argument 3" -> z_][functionName_String]:=...
>
> Is there another way to reach the same goal? Reversing the definition
> of f is of course completely acceptable in most cases. Nevertheless, in
> some cases it may preferrable to view f[functionName] as different
> functions specified by the string functionName, and the arguments to
> these functions should be given in any order.
>
> Thanks in advance,
> Hannes Kessler
>
>


-- 
http://chris.chiasson.name/


  • Prev by Date: Re: Finding paths in graphs
  • Next by Date: Re: how to plot a 2-parametric output from Solve with Plot3D
  • Previous by thread: Orderless attribute for named functions and function arguments
  • Next by thread: Re: Orderless attribute for named functions and function arguments