Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Defining UpValues

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110817] Re: Defining UpValues
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Wed, 7 Jul 2010 07:45:15 -0400 (EDT)

Hi Istvan,

This was a tough one. Here is one way (it took me a while to figure it out,
first I thought it was
not possible, and almost sent you a long explanation why it is
impossible))):

ClearAll[func];
func[x_Integer] :==
  x /; ! MemberQ[Stack[_], HoldForm[func[_Integer][args___]]];
func /: func[sys_Integer][args___] :== sys + 1;
func[sys_Function] :== sys;


In[191]:== {func[1], func[1][2], func[# + 2 &], func[# + 2 &][2]}


Out[191]== {1, 2, #1 + 2 &, 4}

I would still consider changing the design though, since this kind of
difficulties / workarounds usually
indicate to me that the design of the function in question is not well
thought over. For example, if
your sys is often supplied with additional optional arguments, you can
create a wrapper say sysData (or, just
a List) and store them there, like func[sysData[sys,opts___]], etc.

Hope this helps.

Regards,
Leonid


2010/7/6 Istv=E1n Zachar <zac at freemail.hu>

> Dear Group,
>
> consider the following code:
>
> func[sys_Integer] :== sys;
> func /: func[sys_Integer][args___] :== sys + 1;
> func[sys_Function] :== sys;
>
> {
>  func[1],
>  func[1][2],
>  func[# + 2 &],
>  func[# + 2 &][2]
>  }
>
> Is there a way to define func such a way that when the main argument
> is an Integer, any further arguments are ignored? That is, to
> return:    {1, 2, #1 + 2 &, 4}    instead of    {1, 1[2], #1 + 2 &,
> 4}. At present, the second function definition is basically ignored by
> the kernel.
>
> I am aware that func[sys_type, args___] would be the easiest way to
> use here, but in my real code
>    1. there is a large number of named options supplied with sys
> preventing (or at least making hard) the use of *any number* of
> optional second arguments;
>    2. sometimes the main call (func[sys]) is separated from the
> introduction of any optional argument (this is part of a GUI), which
> may cause calls like this:
>    x == func[some_function]; ... (* calculations involving x *) ... ;
> y == x[2];
>
> Thanks in advance,
>
> Istv==E1n
>
>
>
>
>
>
>

--001636c5a74c2f189a048ab6f60d
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable
X-Sun-Content-Length: 2579

Hi Istvan,<br><br>This was a tough one. Here is one way (it took me a while=
 to figure it out, first I thought it was <br>not possible, and almost sent=
 you a long explanation why it is impossible))):<br><br>ClearAll[func];<br>
func[x_Integer] :=<br>  x /; ! MemberQ[Stack[_], HoldForm[func[_Integer=
][args___]]];<br>func /: func[sys_Integer][args___] := sys + 1;<br>func[s=
ys_Function] := sys;<br><br><br>In[191]:= {func[1], func[1][2], func[# =
+ 2 &amp;], func[# + 2 &amp;][2]}<br>
<br><br>Out[191]= {1, 2, #1 + 2 &amp;, 4}<br><br>I would still consider c=
hanging the design though, since this kind of difficulties / workarounds us=
ually<br>indicate to me that the design of the function in question is not =
well thought over. For example, if <br>
your sys is often supplied with additional optional arguments, you can crea=
te a wrapper say sysData (or, just <br>a List) and store them there, like f=
unc[sysData[sys,opts___]], etc.<br><br>Hope this helps.<br><br>Regards,<br>
Leonid <br>
<br><br><div class="gmail_quote">2010/7/6 Istv=E1n Zachar <span dir="lt=
r">&lt;<a href="mailto:zac at freemail.hu">zac at freemail.hu</a>&gt;</span><br=
><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; bord=
er-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Dear Group,<br>
<br>
consider the following code:<br>
<br>
func[sys_Integer] := sys;<br>
func /: func[sys_Integer][args___] := sys + 1;<br>
func[sys_Function] := sys;<br>
<br>
{<br>
 func[1],<br>
 func[1][2],<br>
 func[# + 2 &amp;],<br>
 func[# + 2 &amp;][2]<br>
 }<br>
<br>
Is there a way to define func such a way that when the main argument<br>
is an Integer, any further arguments are ignored? That is, to<br>
return:    {1, 2, #1 + 2 &amp;, 4}    instead of    {1, 1[2], #=
1 + 2 &amp;,<br>
4}. At present, the second function definition is basically ignored by<br>
the kernel.<br>
<br>
I am aware that func[sys_type, args___] would be the easiest way to<br>
use here, but in my real code<br>
    1. there is a large number of named options supplied with sys<br>
preventing (or at least making hard) the use of *any number* of<br>
optional second arguments;<br>
    2. sometimes the main call (func[sys]) is separated from the<br>
introduction of any optional argument (this is part of a GUI), which<br>
may cause calls like this:<br>
    x = func[some_function]; ... (* calculations involving x *) ... ;=
<br>
y = x[2];<br>
<br>
Thanks in advance,<br>
<br>
Istv=E1n<br>
<br>
<br>
<br>
<br>
<br>
<br>
</blockquote></div><br>

--001636c5a74c2f189a048ab6f60d--


  • Prev by Date: Re: Defining UpValues
  • Next by Date: replacement x->y except in Exp[x]
  • Previous by thread: Re: Defining UpValues
  • Next by thread: ParametricPlot3D problem