如何构造 Set 列表
我有一大组参数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题可能还有更多我还没有弄清楚的内容,但这也许会让您走上正轨。
这
会返回一个未计算的“Set”列表,如下所示:
如果您在上面调用 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
returns a list of unevaluated "Set"s like so:
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
当我想要拥有具有副作用的
RuleDelayed
应用程序时,我使用了以下替代解决方案。您可以使用不同的头来替换Set
,直到您的表达式出现在RuleDelayed
表单的右侧(其中它将由保存) RuleDelayed
的HoldRest
属性),然后将Set
替换回去。当我这样做时,我喜欢使用Module
来创建对我来说是一个独特的象征。这样您就不必使用Defer
,这是一个比Unevaluated
更令人不快的难以捉摸的构造。下面是一个示例:
set
符号被赋予与Set
相同的属性以及Unevaluated
存在的原因是为了确保即使有人已经为x
、y
或z
分配了值,这仍然有效。另一种可能性是将所有
Set
表达式包装为闭包,然后在计算RuleDelayed
时使用Scan
调用它们,如下所示: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 forSet
until you have your expression on the right-hand side of aRuleDelayed
form (where it'll be held byRuleDelayed
'sHoldRest
attribute) and then subsituteSet
back in. When I do this, I like to useModule
to create a unique symbol for me. This way you don't have to useDefer
, which is an even more unpleasantly slippery construct thanUnevaluated
.Here's an example:
The reason the
set
symbol is given the same attributes asSet
, and the reason theUnevaluated
is there, is to make sure this works even if someone has already assigned a value tox
,y
orz
.Another possibility is to wrap all your
Set
expressions up as closures and then useScan
to call them when theRuleDelayed
is evaluated, like so: