krlawrence / graph

Practical Gremlin - An Apache TinkerPop Tutorial
Apache License 2.0
830 stars 251 forks source link

Add an example that shows how to use constants with paths #251

Open krlawrence opened 1 year ago

krlawrence commented 1 year ago

In the book, many examples feature the sack step to produce a total distance. In some cases, if all you want is the total, the path step is enough. A by modulator can be used to inject a constant value of zero for path elements that do not have a 'dist' property.

gremlin> g.V(44).repeat(outE().inV().simplePath()).times(2).path().by('code').by('dist').limit(3)
==>[SAF,549,DFW,1197,YYZ]
==>[SAF,549,DFW,1751,YVR]
==>[SAF,549,DFW,4736,LHR]

gremlin> g.V(44).repeat(outE().inV().simplePath()).times(2).path().by(constant(0)).by('dist').limit(3)
==>[0,549,0,1197,0]
==>[0,549,0,1751,0]
==>[0,549,0,4736,0]

gremlin> g.V(44).repeat(outE().inV().simplePath()).times(2).path().by(constant(0)).by('dist').limit(3).sum(lo
cal)
==>1746
==>2300
==>5285