0
$\begingroup$

I have a problems with Evaluate in Manipulate. Here is the code

fls = {"a", "b", "c", "d", "e", "f", "g"};
vars = Table[Symbol["$x" <> ToString@i], {i, fls // Length}];
n = 4;

Manipulate[
  Evaluate@{fls, vars}, 
  Evaluate[Sequence @@ Table[{{vars[[i]], 1, fls[[i]]}, Range[n]}, {i, 1, vars // Length}]]
]

Manipulate[
   Last /@ Normal@GroupBy[Thread[Evaluate@{fls, vars}], Last -> First],
 Evaluate[Sequence @@Table[{{vars[[i]], 1, fls[[i]]}, Range[n]}, {i, 1, vars // Length}]]
]

In the first Manipulate evaluation works, but in the second do not and I do not understand why. Second Manipulate should return {{"a", "b", "c", "d", "e", "f", "g"}} after execution of this code in it

Last /@ Normal@ GroupBy[Thread[{fls, {1, 1, 1, 1, 1, 1, 1}}], Last -> First]

while it returns {{"a"}, {"b"}, {"c"}, {"d"}, {"e"}, {"f"}, {"g"}}. This happens because Evaluate does not work as I suppose.

$\endgroup$
  • $\begingroup$ It seems the Thread introduces the additional braces. Can't you simply use Flatten? As in Flatten[Last /@ Normal@ GroupBy .... $\endgroup$ – anderstood Nov 1 '16 at 19:13
  • $\begingroup$ No I cannot... I need Thread. $\endgroup$ – Филипп Цветков Nov 1 '16 at 19:23
  • $\begingroup$ I meant this Flatten[Last /@ Normal@ GroupBy[Thread[{fls, {1, 1, 1, 1, 1, 1, 1}}], Last -> First]]. $\endgroup$ – anderstood Nov 1 '16 at 19:23
  • $\begingroup$ It is not what I need $\endgroup$ – Филипп Цветков Nov 1 '16 at 19:26
1
$\begingroup$

The symbols in vars (in the body of the second Manipulate) are not the same as the localized vars in the Evaluate[Sequence[..]] declaration. The Evaluate@.. in the body is pretty much a no-op, since it does not appear at the top level (in a function whose arguments are held, which would have to be Manipulate to get them localized properly).

One workaround:

Manipulate[
   Last /@ 
    Normal@GroupBy[foo = Thread[Evaluate@{fls, #}], Last -> First], 
   Evaluate[
    Sequence @@ 
     Table[{{vars[[i]], 1, fls[[i]]}, Range[n]}, {i, 1, 
       vars // Length}]]] &@vars

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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