4

This sounds silly but is there a way to create a empty array inside a Gremlin traversal?

For the query below:

g.V().has('person','name', 'marko').project('a', 'b').by().by()

I want to project b as an empty array. I have tried:

g.V().has('person','name', 'marko').project('a', 'b').by().by(constant("").fold())

But constant("").fold() is not actually empty constant("").fold().count() returns 1. This applies to constant(null).fold() as well.

Glide
  • 20,235
  • 26
  • 86
  • 135

2 Answers2

5

Is this what you are looking for

g.withSideEffect('x',[]).V().has('person','name','marko').project('a','b').by(select('x')).by('name')

==>[a:[],b:marko]
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • This works but just curious, is there a way to have the `withSideEffect` inside the `by`? – Glide Mar 23 '18 at 01:42
4

An empty array/collection would actually be a fold() of nothing. You'll get nothing if you filter everything, hence:

g.V().has('person','name','marko').
  project('a', 'b').
    by().
    by(__.not(identity()).fold())
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34