如何构造 Set 列表

发布于 2024-09-04 06:29:12 字数 579 浏览 3 评论 0原文

我有一大组参数 P,它们采用几组不同的值 V_i,并希望使用 ActionMenu[] 来轻松分配 P=V_i,如下所示:

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

现在的问题是 V_i 的集合很大而且不是静态的,所以不用编码一个长列表 {"opt_1" :>; (P = V_1;),..} 一遍又一遍地手动生成它。

我完全不知道该怎么做。一般方法类似于

Thread@RuleDelayed[listOfNames,listOfActions]

其中 listOfActions 应该类似于

Thread@Set[repeatedListOfP,listOfV_i]

但这不起作用。由于 Set[] 是一个非常特殊的函数,因此我的其他常用方法都不起作用(构建 Table[]、替换标题等)。如何构建 Set[] 操作列表?

I have a large set of parameters P which take several distinct sets of values V_i and want to use ActionMenu[] to make assigning P=V_i easy, like so:

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

Now the problem is that the set of V_i's is large and not static, so instead of coding a long list {"opt_1" :> (P = V_1;),..} over and over by hand, I'd like to generate it.

I am completely stumped at how to do it. The general approach is something like

Thread@RuleDelayed[listOfNames,listOfActions]

where listOfActions should be something like

Thread@Set[repeatedListOfP,listOfV_i]

But this does not work. And since Set[] is a very special function, none of my other usual approaches work (building a Table[], replacing headers, etc). How do you go about constructing a list of Set[] operations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国产ˉ祖宗 2024-09-11 06:29:12

您的问题可能还有更多我还没有弄清楚的内容,但这也许会让您走上正轨。

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

会返回一个未计算的“Set”列表,如下所示:

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

如果您在上面调用 ReleaseHold,则分配实际上会发生。

有关保留和亲属的更多信息,请参见此处:
Mathematica:未评估、延迟、保留、HoldForm、HoldAllComplete、等等

There may be more to your question that I haven't grokked yet but maybe this will get you on the right track.

This

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

returns a list of unevaluated "Set"s like so:

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

If you call ReleaseHold on the above then the assignments will actually happen.

More on Hold and relatives here:
Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc

黎歌 2024-09-11 06:29:12

当我想要拥有具有副作用的 RuleDelayed 应用程序时,我使用了以下替代解决方案。您可以使用不同的头来替换 Set,直到您的表达式出现在 RuleDelayed 表单的右侧(其中它将由 保存) RuleDelayedHoldRest 属性),然后将 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 存在的原因是为了确保即使有人已经为 xyz 分配了值,这仍然有效。

另一种可能性是将所有 Set 表达式包装为闭包,然后在计算 RuleDelayed 时使用 Scan 调用它们,如下所示:

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

Here's an alternative solution that I've used when I've wanted to have RuleDelayed applications that have side-effects. You use a different head to substitute in for Set until you have your expression on the right-hand side of a RuleDelayed form (where it'll be held by RuleDelayed's HoldRest attribute) and then subsitute Set back in. When I do this, I like to use Module to create a unique symbol for me. This way you don't have to use Defer, which is an even more unpleasantly slippery construct than Unevaluated.

Here's an example:

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

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

The reason the set symbol is given the same attributes as Set, and the reason the Unevaluated is there, is to make sure this works even if someone has already assigned a value to x, y or z.

Another possibility is to wrap all your Set expressions up as closures and then use Scan to call them when the RuleDelayed is evaluated, like so:

With[{thunks = MapThread[Function[{a, b}, (a = b) &, HoldAll],
    Unevaluated[{{x, y, z}, {1, 2, 3}}]]},
 "name1" :> Scan[#[] &, thunks]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文