为什么这行不通?选择中的动态 [英] Why won't this work? Dynamic in a Select

查看:26
本文介绍了为什么这行不通?选择中的动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我这样做:

Select[Range[1, 20], # > Dynamic[q] &]

然后我创建滑块:

Slider[Dynamic[q], {1, 20}]

它总是返回一个空集!为什么!

And it'll always return an empty set! Why!

更新这样做的目的是在我移动滑块时更改设置.

Update The goal of this is to have the set change as I move the slider.

推荐答案

关键是要请记住,Dynamic 不直接控制有关评估的任何内容.它的作用是在屏幕上创建一个具有评估属性的点.

The key is to remember that Dynamic does not control anything about evaluation directly. What it does is to create a spot on the screen which has evaluation properties.

例如,如果您要在新的 Mathematica 中评估以下内容会话...

If, for example, you were to evaluate the following in a fresh Mathematica session...

b=5;
Dynamic[a=b];
b=6;
Print[a];

...那么会打印什么呢?与其立即评估,不如想想在你尝试之前.提示...这是一个技巧问题,但要理解技巧会让您对 Dynamic 正在做的事情敞开心扉.

...then what will be printed? Instead of evaluating it immediately, think about it before you try it. Hint...it's a trick question, but understanding the trick will open your mind to exactly what Dynamic is doing.

答案,我不会在这里透露(因为你真的应该尝试一下你自己!)可以用动态从未做任何事情的事实来解释因为它从未出现在屏幕上.分号抑制了屏幕显示Dynamic 的出现,并且没有出现在屏幕上,Dynamic 的评价一事无成.

The answer, which I will not reveal here (because you should really try it for yourself!) can be explained by the fact that the Dynamic never did anything because it never showed up onscreen. The semicolon inhibited the onscreen appearance of Dynamic, and without appearing onscreen, the evaluation of Dynamic accomplishes nothing.

更巧妙的是,如果您删除所有分号,Print[] 语句(位于至少在我的机器上)仍然保持不变,但现在完全不同的原因.那是因为 Dynamic 的屏幕放置保证它的内容将被评估,但不会何时被评估.我的示例设置了一个竞争条件,至少在我的 v7 机器上,Shift+Enter 评估获胜.

More subtly, if you remove all of the semicolons, the Print[] statement (at least on my machine) still remains unchanged, but now for a completely different reason. That's because the onscreen placement of a Dynamic guarantees that its contents will be evaluated, but not when they'll be evaluated. My example sets up a race condition which, at least on my machine in v7, the Shift+Enter evaluation wins.

回到你的例子,

Select[Range[1, 20], # > Dynamic[q] &]

这并不像您认为的那样工作,因为在这种情况下是 Dynamic不评估屏幕上显示的内容.

This doesn't work the way you think it does because the Dynamic in this case isn't evaluating to something which is displayed onscreen.

你可以通过做...来简单地展示结果

You could trivially demonstrate the result by doing...

Dynamic[Select[Range[1, 20], # > q &]]

但我假设您不仅对在屏幕上显示它感兴趣,而且在设置某种副作用.也许您正在将 Select 分配给一个变量.有两种方法可以使这些副作用发生.一种是将它们放入Dynamic 的第二个参数.例如...

but I'll assume you weren't just interested in displaying it on the screen, but in setting some kind of side effect. Perhaps you were assigning Select to a variable. There are two ways to make these side effects happen. One is to put them in the second argument of Dynamic. For example...

findset[x_] := (myset = Select[Range[1, 20], # > x &])
Slider[Dynamic[q, (q=#; myset = findset[q])&], {1, 20}]

第二个是产生一个确实有屏幕外观的动态,但一个这是不明显的.例如,

The second is to produce a Dynamic which does have an onscreen appearance, but one which is not noticeable. For example,

Row[{
    Slider[Dynamic[q], {1, 20}],
    Dynamic[myset = Select[Range[1, 20], # > q &]; ""]
}]

在这种情况下,动态实际上正在显示.它显示在滑块.但是您看不到它,因为它显示的是一个空字符串.尽管如此,它具有任何 Dynamic 所具有的所有自动更新属性.

In this case, the Dynamic actually is displaying. It displays right next to the Slider. But you don't see it because what it's displaying is an empty string. Nonetheless, it has all of the automatic updating properties that any Dynamic has.

有关更多信息,您应该阅读入门和高级动态教程在 Mathematica 文档中.你也可以看到我在 comp.soft-sys.math.mathematica 上的帖子 这里(我在此回复中部分重新制定了该内容).

For more information, you should read the beginning and advanced Dynamic tutorials in the Mathematica documentation. You can also see my post on comp.soft-sys.math.mathematica here (which I partly reformulated for this response).

这篇关于为什么这行不通?选择中的动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆