有没有一种方法可以创建两个压缩的 gremlin 迭代器,其中一个提前一个位置?

Is there a way for creating two zipped gremlin iterators with one advanced one position ahead?

我正在尝试以编程方式向图形添加边。

我的想法是,我将压缩两个顶点流,如果连续两个顶点满足某些条件,我将在它们之间创建一条边。

问题在于压缩。我无法压缩两个流,其中一个流提前一位。

(
    g.V().hasLabel("person").
    order().by("age")
    .as("x")
    .local(
        union(
            select("x"),
            select("x") // .skip(1)
        ).fold()
    )
)

==>[v[2],v[2]]
==>[v[1],v[1]]
==>[v[4],v[4]]
==>[v[6],v[6]]

以上作品。但是当我取消注释 skip 部分时,我得到

==>[v[2]]
==>[v[1]]
==>[v[4]]
==>[v[6]]

但我需要:

==>[v[2],v[1]]
==>[v[1],v[4]]
==>[v[4],v[6]]

我附上后续部分以供参考。

(
    g.V().hasLabel("person").
    order().by("age")
    .as("x")
    .local(
        union(
            select("x"),
            select("x")
        ).fold()
    ).sideEffect(
        project("first", "second")
            .by(unfold().limit(1))
            .by(unfold().skip(1))
        .coalesce(
            select("first").out("age_lt").unfold(),
            choose(
                math("second - first").by("age").is(lt(5)),
                addE("age_lt").from(select("first")).to(select("second"))
            )
        )
    ).none()
)

这将为您提供成对的连续顶点:

g.V().hasLabel("person").order().by("age").store("x").local(
    select(all,"x").tail(local, 2)
).skip(1)

注意最后的跳过以过滤掉第一个单个顶点"pair"。

您可以使用具有这种形式的分区模式来做到这一点(其中“2”是您的分区大小):

g.V().fold().
  emit().
  until(__.not(__.unfold())).
  repeat(__.skip(local, 2)).
  filter(__.unfold()).
  limit(local, 2)

但不是在 repeat() 中跳过“2”,而是跳过“1”,因为您只想将遍历器向前推进“1”,因此:

gremlin> g.V().hasLabel("person").
......1>   order().by("age").fold().
......2>   emit().
......3>   until(__.not(__.unfold())).
......4>   repeat(__.skip(local,1)).
......5>   filter(__.unfold()).
......6>   limit(local,2)
==>[v[2],v[1]]
==>[v[1],v[4]]
==>[v[4],v[6]]
==>[v[6]]

然后,删除任何尾随的未配对项:

gremlin> g.V().hasLabel("person").
......1>   order().by("age").fold().
......2>   emit().
......3>   until(__.not(__.unfold())).
......4>   repeat(__.skip(local,1)).
......5>   filter(__.unfold()).
......6>   limit(local,2).
......7>   filter(count(local).is(2))
==>[v[2],v[1]]
==>[v[1],v[4]]
==>[v[4],v[6]]