Re: Displaying a list of variables together with their names
- To: mathgroup at smc.vnet.net
- Subject: [mg111195] Re: Displaying a list of variables together with their names
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Fri, 23 Jul 2010 07:12:00 -0400 (EDT)
Hi Szabolcs,
>
>
> My Mathematica skills got a little rusty
You are kidding, right?
Seriously, I don't see an easy solution which would be much simpler
conceptually -
looks like some number of Unevaluated will be needed anyways in this
approach.
You can save some keystrokes by using Block trick though:
ClearAll[dynamicVariableDisplayAlt];
SetAttributes[dynamicVariableDisplayAlt, HoldAll];
dynamicVariableDisplayAlt[vars__] :=
Dynamic@Grid[Block[{vars}, {SymbolName[#], #} & /@ {vars}],
Alignment -> {{Left, "."}}];
Here I exploited the way name conflicts are resolved when RuleDelayed is the
outer
scoping construct - namely that it does not respect inner scoping constructs
(Block here).
So, while you get a warning by {vars} painted in red inside Block, it will
work all right.
You can also make this a pure function, which is a little more compact:
dynamicVariableDisplayPure =
Function[Null,
Dynamic@Grid[Block[{##}, {SymbolName[#], #} & /@ {##}],
Alignment -> {{Left, "."}}], HoldAll];
This example is actually quite interesting, since here, while Function is a
lexical scoping
construct, with slots is shows macro-like behavior. And I found very
puzzling why this
version with fixed number of named arguments also works:
dynamicVariableDisplayPure3Args =
Function[{x, y, z},
Dynamic@Grid[Block[{x, y, z}, {SymbolName[#], #} & /@ {x, y, z}],
Alignment -> {{Left, "."}}], HoldAll];
For this, I have no explanation. I would expect Function to respect Block
and therefore the names
of the variables to be {"x","y","z"} and not {"a","b","c"}. But Function
acted as ruthlessly as
RuleDelayed here. Actually, the same can be seen on a much simpler
example Function[x, Block[{x}, Print[x]]][1].
Regards,
Leonid
, I was wondering what
> is a simpler / better way to achieve it than this:
>
> SetAttributes[dynamicVariableDisplay, HoldAll]
> dynamicVariableDisplay[vars__] :=
> Dynamic@Grid[
> Transpose[{{Function[x, SymbolName[Unevaluated[x]], {HoldAll}] /@
> Unevaluated[vars]}, {vars}}], Alignment -> {{Left, "."}}]
>
> a=1; b=2; c=3;
>
> dynamicVariableDisplay[a, b, c]
>
> There's too much juggling with unevaluated symbols.
>
> Thanks for the help in advance,
> Szabolcs
>
>