Sequential edge creation between streamed vertices

I would like to create an edge between vertices as they are streamed in sequence from a traversal. I want to connect each vertex to the next one in the stream, like a linear chain of vertices with a
next
edge.

For example, given this g.V().hasLabel("person").values("name") produces:
==> josh
==> peter
==> marko
==> vadas

I'd like to achieve something like josh -next-> peter -next-> marko -next-> vadas

My current approach is to create a "sliding window" of vertex pairs:
g.V().
  hasLabel("person").
  aggregate(local, "a").by("name").
  map(select("a").tail(local, 2)).
  skip(1)

Which results in:
==> [josh, peter]
==> [peter, marko]
==> [marko, vadas]

Next I add edges between each pair:
g.V().
  hasLabel("person").
  aggregate(local, "a").
  map(select("a").tail(local, 2)).
  skip(1).
  addE("next").from(limit(local, 1)).to(tail(local, 1))

This works, but I’m not sure if this is the idiomatic way to do this. I think modeling data where vertices are explicitly ordered through an edge connection is not uncommon, right?
Solution
i think that's about as good as most approaches. we're missing a step that coudl simplify this code though. i've long wanted a partition() step so that you could change all that code to just:
g.V().hasLabel('person').
  partition(2).
  addE('next').from....
Was this page helpful?