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

Orderless attribute for named functions and function arguments

  • To: mathgroup at smc.vnet.net
  • Subject: [mg72525] Orderless attribute for named functions and function arguments
  • From: "Hannes Kessler" <HannesKessler at hushmail.com>
  • Date: Thu, 4 Jan 2007 07:04:24 -0500 (EST)

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


  • Prev by Date: Re: List representation using element position
  • Next by Date: Re: Finding paths in graphs
  • Previous by thread: Re: Re: Finding paths in graphs
  • Next by thread: Re: Orderless attribute for named functions and function arguments