Use of local() Revisited
In recent weeks there have been many questions about the local()
-step both in Discord and in TinkerPop’s JIRA. I’d written on this topic once before in Use of local() but it perhaps begs a further revisit. The example that arose from Discord helped inspire this post as it was sufficiently primitive to hopefully clarify usage.
Here’s the primitive example:
We’d expect the last traversal to behave the same as the other two. The strangest looking thing here is that there are just two results, when we clearly started with three lists that were given to inject()
. The local()
-step covers a fairly narrow and yet potentially helpful use case, in that it is meant to be used in cases where you want object-local computations. By object-local we’re talking about a particular object and not the objects in the stream as a whole. As a result, the two [1,2]
lists which have an equality end up counting to the same object and we get “4”. Demonstrated another way:
When I see a traversal with local()
in it, I always question if it is being used properly. More often than not, folks tend to use local()
when they instead intend Scope.local
processing, as in count(local)
in this case or a map()
or flatMap()
operation. Always prefer those options, unless you are sure that object-local processing is what you need in your computation.