使用分号语法了解评估和执行顺序 [英] Understanding the evaluation and execution order with semicolon syntax
问题描述
我已经从我的上一个问题中学到了一些关于FFL分号的信息.但是,仍不清楚它们执行什么评估或执行顺序.因此,这是一个更具体的示例:
I already learned a little bit about FFL semicolons from my previous question. However, it is still not clear what order of evaluation or execution they enforce. So here is a more concrete example:
[ expr_a, expr_b ; expr_c, expr_d ; expr_e, expr_f ]
以上代码的执行顺序应该是什么?在我的脑海中应该是:
What should be the order of execution for the above code? In my head, it should be:
- 评估& b
- 执行a,执行b
- 评估c& d
- 执行c,执行d
- 评估e& f
- 执行e,执行f
现在让我们想象一下expr_b = add(test_list, ['b saw ' + str(test_list)])
以及所有其他表达式的相似之处.那么test_list
的最终内容是什么?
Now let's imagine that expr_b = add(test_list, ['b saw ' + str(test_list)])
and similar for all the other expressions. Then what would be the final contents of test_list
?
在我的脑海中应该是:
锯[]
b看到了[]
c看见了[a看见了[],b看见了[]]
c saw [a saw [], b saw []]
d锯[a锯[],b锯[]]
d saw [a saw [], b saw []]
e锯[a锯[],b锯[],c锯[a锯[],b锯[]],d锯[a锯[],b锯[]]]
e saw [a saw [], b saw [], c saw [a saw [], b saw []], d saw [a saw [], b saw []]]
f锯[a锯[],b锯[],c锯[a锯[],b锯[]],d锯[a锯[],b锯[]]]
f saw [a saw [], b saw [], c saw [a saw [], b saw []], d saw [a saw [], b saw []]]
请解释为什么不是这种情况.
Please explain why that is not the case.
推荐答案
首先,您可能不想编写完全像这样的代码.通常,分号的优先级非常低,但是列表文字不是运算符,并且代码将如下所示:
To begin with, you probably don't want to write code exactly like this. Generally, semi-colons have very low precedence, but a list literal isn't an operator, and the code will be seen like this:
[a, (b; c), (d; e), f]
这意味着您将并行启动四个命令管道(尽管其中两个只有一个成员).它将评估a
,b
,d
,f
.然后它将执行a
的结果,然后是b
的结果.执行b
将触发命令管道中的下一步,因此它将评估并执行c
.然后将执行d
,然后求值并执行e
,最后将执行f
.
This means that you are starting off four command pipelines in parallel (though two of them only have a single member). It will evaluate a
, b
, d
, f
. Then it will execute the results of a
, then the results of b
. Executing b
will trigger the next step in the command pipeline, so it will evaluate and execute c
. Then it will execute d
, then evaluate and execute e
, finally it will execute f
.
所以:
a saw []
b saw []
c saw [a saw [], b saw []]
d saw []
e saw [a saw [], b saw [], c saw [a saw [], b saw []], d saw []]
f saw []
这篇关于使用分号语法了解评估和执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!