Skip to content

Instantly share code, notes, and snippets.

@webwurst
Created August 26, 2011 22:04
Show Gist options
  • Save webwurst/1174536 to your computer and use it in GitHub Desktop.
Save webwurst/1174536 to your computer and use it in GitHub Desktop.
Gremlin - nestedLeafs, nestedAll
// Tree-Structure from https://gremlin-users.googlegroups.com/attach/eb75b39c6330a874/Simple+Tree.png?pli=1&view=1&part=4
g = new TinkerGraph()
A = g.addVertex('A')
B = g.addVertex('B')
g.addEdge(A, B, 'child')
C = g.addVertex('C')
g.addEdge(A, C, 'child')
D = g.addVertex('D')
g.addEdge(B, D, 'child')
E = g.addVertex('E')
g.addEdge(B, E, 'child')
F = g.addVertex('F')
g.addEdge(B, F, 'child')
G = g.addVertex('G')
g.addEdge(D, G, 'child')
H = g.addVertex('H')
g.addEdge(F, H, 'child')
root = A
// Step nestedLeafs
nestedLeafs = {
_().as('l').out.loop('l'){!!it.object.out}
}
Gremlin.defineStep("nestedLeafs", [Vertex,Pipe], nestedLeafs)
root.nestedLeafs.paths
// Step nestedAll
_nested = { seen ->
x << seen
if (seen[-1].out)
seen[-1].out.toList().each{_nested(seen+it)}
}
nestedAll = {
_().transform{
x = []
_nested([it])
return x
}
}
Gremlin.defineStep("nestedAll", [Vertex,Pipe], nestedAll)
root.nestedAll >> 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment