对于程序语言Ruby,Python, Groovy的使用者来说,map步骤是相当熟悉的。它常用于保存结果集,或者在小精灵中,保存图遍历的当前状态,在把当传给遍历的下一部分前可以进行一些修改。这些都是map步骤可以让我们做的。
The map step will be familiar to users of programming languages such as Ruby, Python or indeed
Groovy. It is often useful to be able to take a set of results, or in the case of Gremlin, the current
state of a graph traversal, and modify it in some way before passing on those results to the next
part of the traversal. This is what the map step allows us to do.
map步骤可以把遍历或者闭包当做它的输入。遍历的结果或者闭包,会传到整个遍历的下个步骤。下面是一个map步骤的简单的例子,用遍历来修改传递的内容。
The map step can accept a traversal or a closure as input. The results of the traversal or closure will
be passed on to the next step in the overall traversal. Below is a simple example of a map step using
a traversal to modify what is passed on.

 

运行这个查询,返回的结果是被选择的10个机场每个机场顶点的属性。

When this query is run the output returned is the selected vertex properties for each of the 10
airports that were selected.

 

我们可以再进一步,用map步骤给我们生成一个键值的map.

We could go one step further and have the map step produce a key and value map for us.

 

就如笔者提上边提到的,在很多情况下,一个map步骤可以用在查询的中间,改变传递到下一部分的内容。下面这个例子取出了map步骤的输出,基于城市的名称,进行降序排序。显然,有相似的方式,我们可以编写这个查询,但是这个例子说明了map步骤可以做的相当好。

As I mentioned above, in many cases, a map step is used in the middle of a query to change what is
passed on to the next step. The example below takes the output from the map step and sorts the
results in descending order based on the city names. Obviously there are simpler ways we could
write this query but this demonstrates what map does quite well.

 

运行查询,输出在这,显示出了按序排列的城市名。

Here is the output from running the query showing the sorted city names.

 

一些情况下,有其它的步骤,比如value步骤可以用做是一种缩略形式的map步骤。下面的两个查询得到了相同的结果。如果缩略形式存在,就不需要强制写一个map步骤。

In some cases there are other steps, such as the values step that can be used as a shorthand form of
a map step. The following two queries yield the same results for example. There is no need to write
an explicit map step when a shorthand form exists.

 

现在看一个使用了lambad函数的例子。首先看下面的查询,它返回了一个包含图中前10个机场ID的列表。想像一下,我们想写一个查询,类似上面这个,它将会修改上面的查询得到的个ID,为每个ID加1. 看下面的查询。我们已经介绍了map步骤。map步骤可以做为闭包函数的参数,告诉它我们想让它对输入的值怎么处理。这是个groovy句法“输入的东西(在这个例子中是一个遍历)”get方法需要得到访问当前的顶点和它的属性。最后我们得到了顶点的ID并对它加1.这个修改后的值会传递到遍历的下一步,它们会通过fold步骤进入一个列表。

Now let’s look at an example where a lambda function (closure) is used. First of all, take a look at
the query below. It simply returns us a list containing the IDs of the first ten airports in the graph.
Imagine we wanted to write a query, similar to the one above, that will modify each of those IDs
returned in the query above by adding one to each. Take a look at the query below. We have
introduced a map step. The map step takes as a parameter a closure (or lambda) function telling it
how we want it to operate on the values flowing in to it. The it is Groovy syntax for "the thing that
came in" (in this case a traversal). The get is needed to gain access to the current vertex and its
properties. Lastly we get the id of the vertex and add one to it. The modified values are then passed
on to the next step of the traversal where they are made into a list by the fold step.

 

这部分Groovy和Java有相似但是不同的句法。如果您想要在Java和序中使用上面的查询,您需要使用java的lambada函数句法。

This is an area where Groovy and Java have a similar but different syntax. If you
wanted to use the query above in a Java program you would need to use the Java
lambda function syntax.
map步骤好的一点是,它允许我们在查询中这么做,否则我们就得在查询后过度地使用每个类型的for loop循环结构。
What is nice about the map step is that it allows us to do within the query itself what we would
otherwise have to do after the query was over using a for each type of loop construct.

 

 

另一件map步骤要注意的事情是:闭包可以有多个步骤,用分号分隔。下面的查询说明了这一点。
One other thing to note about the map step is that the closure provided can have multiple steps,
separated by semi-colons. The following query demonstrates this.

 

注意闭包中的最后一个表达式的值是从map返回的。上面的例子的结是查c=a+b,3,它被加到了每个ID上。

Note that only the value from the last expression in the closure is returned from the map. So in the
example above the result of c=a+b, 3, is added to each ID.
如我们所见,在使用小精灵时,有多种方式可以实现相同的结果。在性能或其它维度,一些方式总是优于其它方式。在“sack做为一种存值的方式介绍”这一节中我们会用sack步骤来向结果集增加1,像下面这样。
As we have already seen, there are often multiple ways to achieve the same result when working
with Gremlin. It is also true that some ways are almost always better than others in terms of
performance or some other metric. In the "Introducing sack as a way to store values" we used a
sack step to add one to a set of results as follows.

 

我们可以用map步骤得到相同的结果。然而这么做就会需要使用闭包,这是使用sack的版本可以避免的。在小精灵中,避免不必要的闭包的使用是最佳实践。

We could achieve the same result using a map step. However, doing so introduces the need to use a
closure which the version using sack avoids. Avoiding unnecessary use of closures is a Gremlin best
practice.
在下一节中我们会看到一些lambada表达式使用的情况,也有很多使用map步骤的例子。
In the next section we will take a look at some other cases where lambda expressions are very
useful as well as a few more examples of the map step being used.
posted on 2022-04-25 11:04  bokeyuannicheng0000  阅读(27)  评论(0)    收藏  举报