0

I am using Gremlin-Scala and I have the following code:

val paths = w.as("a").out("next").jump(
  to = "a",
  jumpPredicate = { t: Traverser[Vertex] =>
    t.loops < 5
  }
).path.toList

I don't know when my loop will finish, so I have no access to the size and if I put wrong number for x in t.loops > x then I will have problem. I changed my code in following and it works well. it has two problems (1- it's ugly 2- I have to call get() which I think is not efficient) Is there better way to do this?

val paths = w.as("a").out("next").jump(
  to = "a",
  jumpPredicate = { t: Traverser[Vertex] =>
    t.get().out("next").size > 0
  }
).path.toList
Omid
  • 1,959
  • 25
  • 42

1 Answers1

0

Re your problems:

1: it's ugly - what exactly do you dislike about it? You could leave out the type annotation and variable names...

2: there's nothing inefficient about .get there - it's a simple getter on the traverser to get the element. jump gives you the traverser instead of the element so that you have more context - the traverser e.g. holds information about how many loops you've done so far, the path, etc.

This should work: val paths = w.as("a").out("next").jump( "a", _.get().out("next").size > 0 ).path.toList

Another loop example (finding the shortest path): http://www.michaelpollmeier.com/2014/12/27/gremlin-scala-shortest-path/

Michael Pollmeier
  • 1,370
  • 11
  • 20