Consider the following code:

ClearAll[x, y]
x = y;
y = 2;
?x
?y

This will store $x$ being equal to the variable $y$, and $y$ having the value of $2$.

Now switch the assignment lines:

ClearAll[x, y]
y = 2;
x = y;
?x
?y

the internal representation will be $x=2$, $y=2$, since at the time of the assignment $x=y$, the value of $y$ is known and used for the assignment, not the variable $y$ itself, as it has been in the first case.

Now consider the case where I've built up a long code with complicated expressions being substituted into each other, and at some final point I would like to boil this down as much as possible.

Stated differently: How would I make sure $x$ is updated to the value of $y$ in the first example instead of still referring to $y$?

link|improve this question

47% accept rate
these two pieces of code do get the the same result via a different route, if you want the second behavior use the second version. As you noted your self x=x will solve your "problem". – ruebenko Feb 20 at 8:49
feedback

3 Answers

up vote 4 down vote accepted

How about defining this function

SetAttributes[updateSymbols, HoldAll]
updateSymbols[syms__] := 
   Scan[Function[x, If[ValueQ[x], x = x], HoldAll], Hold[syms]]

then running

updateSymbols[x, y]

after the definitions have been made?

It will redefine each symbol, evaluating the RHS of their definitions.

Note: Only works for OwnValue-symbols, and I'm not entirely sure it can't break something.


Usage example:

x=y;
y=2;

?x
(* ==>
 Global`x
 x=y
*)

?y
(* ==>
Global`y
y=2
*)

updateSymbols[x,y]

?x
(* ==>
Global`x
x=2
*)

?y
(* ==>
Global`y
y=2
*)
link|improve this answer
feedback

Use SetDelayed in place of Set in all assignments where you want your updating to happen:

x:=y
y:=2
...

Better still, use functions rather than variables and pass values locally, this will be cleaner.

EDIT

If you really want to "hard-code" to the final values, you either need to have a list of symbols you want, or get it somehow. Here is a possibility:

Cases[Names["Global`*"],
   name_ :> With[{heldSym = ToExpression[name, InputForm, HoldComplete]},
        Set @@ Join[heldSym, heldSym] /; OwnValues @@ heldSym =!= {}]]

This will "hard-code" all global symbols which have OwnValues, to their current values.

EDIT 2

Here is a local version of my code:

ClearAll[update];
SetAttributes[update, HoldAllComplete];
update[syms__Symbol] := Cases[Unevaluated[{syms}], s_ :> (s = s) /; OwnValues[s] =!= {}];
link|improve this answer
That won't solve the problem: x:=y; y:=2 will not assign the value 2 to x once it is known. Of course, using x (in both my and your case) will print 2, but I'm looking to update the definition itself, as in "x will have ownvalue 2". – David Feb 19 at 21:00
@David Ok, I see. Please see my edit. If you have an explicit list of symbols (perhaps, symbols wrapped in Hold or HoldComplete), this would be even easier. – Leonid Shifrin Feb 19 at 21:09
Leonid, I fear I've asked this before, but why don't you use MakeExpression[name] in this kind of application? – Mr.Wizard Feb 19 at 21:54
@Mr.Wizard For no good reason, actually. You are right, it is a better fit here. – Leonid Shifrin Feb 19 at 22:00
feedback

As stupid as it sounds, x = x seems to work:

ClearAll[x, y]
x = y;
y = 2;

?x
?y
(* => x = y, y = 2 *)

x = x;
?x
(* => x = 2 *)

This basically evaluates x, and then assigns the result to x again. Written more explicitly, x = x is the same as x = Evaluate[x].

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.