Why does these two yield different results?
graph.traversal()
.V().map(__.out("contains"))
.valueMap(true).next(100)
compared to
graph.traversal()
.V().out("contains")
.valueMap(true).next(100)
Why do I prefer map to directly calling the .out() method? This way I can organize my code where I can get traversals from methods and "map" to existing traversals.
In thinking about this issue, recall that Gremlin is a bit like a processing pipeline, where objects are pulled through each step in the pipeline to apply some transformation, filter, etc. So, given your example in its most simplistic form you would say that you are getting all vertices and traversing on
out()
edges, which means that you are comparing the following traversals and results:Those traversals return two different results because you are asking Gremlin for two different things. In the first case,
out()
is not a form ofmap()
, it is a form offlatMap()
which means that for each vertex traverser coming through the pipeline it will iterate all the outgoing edges and traverse to and return the adjacent vertex (i.e. one-to-many transform). In the second case, you are asking Gremlin to do a simplemap()
of a vertex to another object (i.e. one-to-one transform) which in this case would be the result ofout()
which is the first object in that traverser stream.To demonstrate you could simply change
map()
toflatMap()
as follows:or alternatively
fold()
the results ofout()
to a single object to maintain the one-to-one transform logic: