sack步骤,如下面的例子所示:是一个副作用的步骤,这就是说,它会在遍历期间存储值,不会影响下向一步传递的内容。
The sack step, as shown in the examples below, is a side effect step, meaning it can store values
during a traversal but has no effect on what is passed on to the next step.
顺带介绍一下,看下面这个例子。所有的查询就如它们表示的一样,是创建一个您可以从SAF出发到达的每个机场跑道数的列表。我们还没有引入sack步骤。
By way of an introduction, take a look at the example below. All the query does as it stands is create
a list of the number of runways that each airport that you can fly to from Santa Fe (SAF) has. We
have not introduced a sack step into the equation yet.

 

现在我们开始把sack的使用引入到查询中。在使用sack时, 我们典型的处理是以一些方式初始化sack.下面的借子用0初始化了一个sack.但没有用它做别的事,所以它的结果没有改变。

Now let’s start to introduce some usage of sacks into the query. When working with a sack we
typically initialize the sack in some way. The example below initializes a sack with a value of zero
but does not yet do anything with it so the result is unchanged.

 

这次我们用加法运算符把跑道数加到sack中,但是我们的初始值是0,我们实际上没有改变最终的结果。这是因为我们实质上进行的运算是0与每个机场的跑道数相加。最后调用不带参数的sack,sack现在的内容被返回。fold 步骤和前边一样,就把我们的结果放在一个列表中。

This time we add the runways to our sack using a sum operation, but as our starting value is zero
we are not actually changing the end result in any way. This is because we essentially perform the
operation 0 + runways for each runway value. The final call to sack with no parameters causes the
current contents of the sack to be returned. The fold step, as before, puts whatever results we got
from the sack into a list.

 

现在我们做点事情来改变结果。让我们把初始值设置为1而不是0,看看会发生什么。

So let’s finally do something that will change the result. Let’s make the starting value one rather
than zero and see what happens.

 

现在,使用求和操作符,每个机场的跑道数都和sack相加,执行的运算是跑道数加上1.从结果中您可以看到,每一条记录,返回的值都比之前的值大了1. 这是一个简单的例子,但是相信您已经明白了如何使用sack了。

Now, each time the number of runways was added to the sack using a sum operator, the operation
that was performed was 1 + runways. As you can see from the results, in each case, the value
returned is one higher than those from the previous query. This is a very simple example but
hopefully you can start to see how useful sacks can be.
在进入到更多有趣的例子之前,有一些事要说明:您也可以用assign操作符来初始化一个sack,像下面这样。重要的是要注意:按这种方式,sack的初始化不一定发生在查询的开始。在下面的例子中,用到了常量1,但是我们也可以用一个遍历来初始化sack,就像我们在接下来的例子中看到的一样。
Before getting into some more interesting examples, it is worth pointing out that you can also
initialize a sack using the assign operator as shown below. It is also important to note that this sack
initialization does not have to happen at the start of the query when done in this way. In the
example below, a constant value of one is used, but we could equally well have used a traversal to
initialize that sack as we shall see in the next example.

 

现在让我们的查询更有趣一些。下面查询是一些有趣的新的调整。首先,用AUS顶点的跑道数来初始化sack,而不是像以前一样只用一个常量。第二,注意在与sack相加时没有明确调用values, 我们只用了一个by调节器来说明我们想要把什么与sack相加。

Let’s now make our query a bit more interesting. There are a couple of interesting new twists
shown in the query below. Firstly, the sack is initialized to contain the number of runways from the
AUS vertex whereas before we just used a simple constant. Secondly, notice that rather than make
an explicit call to values before adding to the sack, we can just use a by modulator to specify what
we want added to our sack.

 

这次,因为AUS机场有两条跑道,我们生效的计算变成了2加上跑道数。

This time, as the Austin (AUS) airport has two runways our calculation in effect became 2 +
runways.
在看一些更复杂的使用了多个sack步骤的查询之前,我们应当看一下其它的一些运算符。到目前为止, 我们只用了求合sum.下面的查询用到了mult相乘,和它的名字说明的一样,它是把多个值乘起来,而不是把它们加起来。
Before looking at some slightly more complex queries that use multiple sack steps, we should take a
look at some of the other operators. So far we have just used sum. The query below uses mult which
as its name implies will multiply the values together rather than add them.

 

类似地,减号会在把它们放到sack之前减少。注意是从sack的初始值上去减除它们。

Likewise, minus will subtract the values before putting them into the sack. Note that the values are
subtracted from the sack’s initialization value.

 

如果我们想从其它的值上减去sack的初始值,我们可以用负值来初始化它,然后再进行加法运算。

If we wanted to subtract the sack’s initial value from the other values we can simply initialize it
with a negative value and perform a sum operation.

 

 

posted on 2022-04-24 13:20  bokeyuannicheng0000  阅读(36)  评论(0)    收藏  举报