How to Build a List Set

I have a large set of P parameters that take several different sets of V_i values ​​and want to use ActionMenu[]to simplify the assignment of P = V_i:

ActionMenu["Label", {"name_1" :> (P = V_1;),..}]

Now the problem is that the set V_i is large and not static, so instead of manually rewriting the long list {"opt_1" :> (P = V_1;),..}manually, I would like to generate it.

I am completely fixated on how to do this. The general approach is something like

Thread@RuleDelayed[listOfNames,listOfActions]

where listOfActionsshould be something like

Thread@Set[repeatedListOfP,listOfV_i]

But that does not work. And since it Set[]is a very special function, none of my other conventional approaches work (building Table[], replacing headers, etc.). How are you going to create a list of operations Set[]?

+3
2

, grokked, , , .

MapThread[Hold[#1 = #2]&, {{a, b, c}, {1, 2, 3}}]

"Set" s :

{Hold[a = 1], Hold[b = 2], Hold[c = 3]}

ReleaseHold , .

:
Mathematica: Unevaluated vs Defer vs Hold HoldForm HoldAllComplete vs .. ..

+2

, , RuleDelayed, . Set, RuleDelayed ( RuleDelayed HoldRest), Set . , Module . , Defer, , Unevaluated.

:

Module[{set},
 Attributes[set] = Attributes[Set];

 With[{rhs = MapThread[set, Unevaluated[{{x, y, z}, {1, 2, 3}}]]},
  "name1" :> rhs /. {set -> Set, List -> CompoundExpression}]]

Set , Set, , Unevaluated, , , , - x, y z.

, Set , Scan , RuleDelayed, :

With[{thunks = MapThread[Function[{a, b}, (a = b) &, HoldAll],
    Unevaluated[{{x, y, z}, {1, 2, 3}}]]},
 "name1" :> Scan[#[] &, thunks]]
+1

Source: https://habr.com/ru/post/1748602/


All Articles