MathGroup Archive 2004

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

Search the Archive

RE: List of Variables and manipulating them ...

>-----Original Message-----
>From: Detlef Mueller [mailto:dmueller at]
To: mathgroup at
>Sent: Wednesday, March 17, 2004 1:55 AM
>To: mathgroup at
>Subject: [mg46966] [mg46935] List of Variables and manipulating them ...
>I have the following Idea:
>During a Session, a User can create some Variables, wich are
>all of the Form 
>x = MyType[L_list, S_anything].
>and so on.
>Now I want to perform the following:
>Take All Variables wich look like 
>MyType[L, S] and replace L by some Function
>of L, for example Sort L descending instead
>of ascending.
>For the given Example, after calling
>the Variables should changed and have the 
>and so on.
>Any Ideas, if and how this could be
>(The Problem appeared while creating Objects enriched
>with some Information in them, as a Term-Order. Now 
>I want to change this Order, and update all existing
>objects ...)
>  Detlef Mueller

I'm not too sure in this, to me it appears as you were changing a type. The
basic question seems to be if you'll like that, e.g. if the girl you
married, suddenly changed here type, or your bank, imagine!

One idea to pursue your program (literally) would be to locate all
definitions which have MyType[...] at there rhs, and change them.

x = MyType[{1, 2, 5, 6}, Y];
y = MyType[{1, 2, 4, 9}, R]; 
A99 = MyType[{1, 2, 99}, Pi];

In[3]:= allNames = Names["Global`*"];

In[4]:= pos = Position[Symbol /@ allNames, MyType[__]]
Out[4]= {{1}, {5}, {6}}

In[5]:= thoseNames = Extract[allNames, pos]
Out[5]= {"A99", "x", "y"}

In[6]:= thoseValues = Symbol /@ thoseNames
{MyType[{1, 2, 99}, \[Pi]],
 MyType[{1, 2, 5, 6}, Y],
 MyType[{1, 2, 4, 9}, R]}

In[7]:= Clear /@ thoseNames;

MapThread[(#1 = MapAt[Reverse, #2, 1]) &,
          {Symbol /@ thoseNames, thoseValues}];

In[9]:= Symbol /@ thoseNames
{MyType[{99, 2, 1}, \[Pi]],
 MyType[{6, 5, 2, 1}, Y],
 MyType[{9, 4, 2, 1}, R]}

Such you have got what you wanted. Yet I doubt you will become happy with
that. It's a bit expensive to this (this can be mitigated a bit, if you keep
all your symbols denotating your "objects" within a certain Context). But
this appears not to be the object-oriented way: you had to define Methods to
access Parts of your objects, which is easy to do if you can keep a
discipline not to use Part, First, Extract, ReplacePart,... or pattern
matching to do that (except within these methods); but as you see, then you
give away much of Mathematica.

Alternatively, if your manipulations are confined to a certain stretch of
code, then localize this with Block and try something like this.

Set up, temporarily, a definition for MyType, that delivers the reversed
order (or function value) 

MyType[aa_List, b_] := YourType[Reverse[aa], b];
MyType[a_, b_] := YourType[a, b]

In[9]:= Part[MyType[{1, 2, 3}, Gaga], 1, 1]
Out[9]= 3

In[10]:= First[MyType[{1, 2, 3}, Gaga]]
Out[10]= {3, 2, 1}

In[13]:= MyType[{1, 2, 3}, Gaga] /. MyType[a_, b_] :> a
Out[13]= {3, 2, 1}

... but 

In[16]:= Unevaluated[MyType[{1, 2, 3}, Gaga]] /. HoldPattern[MyType[a_, b_]]
:> a
Out[16]= {1, 2, 3}

Done that, clear the definitions for MyType (or leave Block)

Hartmut Wolf

  • Prev by Date: RE: doing things on a procedural way and doing them on a functional way
  • Next by Date: RE: List of solution
  • Previous by thread: List of Variables and manipulating them ...
  • Next by thread: Re: List of Variables and manipulating them ...