best way to check for empty pipe when returning value

409 views
Skip to first unread message

ziggy

unread,
Jun 23, 2014, 9:02:32 PM6/23/14
to gremli...@googlegroups.com
Hi, I've got to ask what may be a simple question.  When doing a Gremlin query, I want to check to see if the pipe is empty before returning a value.  Is there a simple way to check for an empty pipe and return a pre-specified value if it's empty?  I must be missing something

# can't do this if the pipe empties out before it hits the next()
output = v1.inE('LINKTYPE').outV().next().name

# return value or null if pipe empty (what a mess and it fails in some cases)
output=null; v1.inE('LINKTYPE').outV().sideEffect{output=it.name?it.name:null}

# this works, but even more ugly
nodes=[]; v1.inE('LINKTYPE').outV().fill(nodes)
output = (nodes.size > 0)?nodes[0].name:null 

# something like this would be ideal
output = v1.inE('LINKTYPE').outV().nextValOrElse('name', 'MISSING')

thanks for reading this

Daniel Kuppitz

unread,
Jun 23, 2014, 10:06:57 PM6/23/14
to gremli...@googlegroups.com
Simply do this:

pipe = v1.in('LINKTYPE')[0].name
output = pipe.hasNext() ? pipe.next() : null

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Louis Clark

unread,
Jun 24, 2014, 1:19:23 PM6/24/14
to gremli...@googlegroups.com
much better, thanks Daniel.



--
You received this message because you are subscribed to a topic in the Google Groups "Gremlin-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gremlin-users/ICHU5cEGfeM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gremlin-user...@googlegroups.com.

Andras Gefferth

unread,
Jun 24, 2014, 6:54:39 PM6/24/14
to gremli...@googlegroups.com
+1 for the question and +1 for the answer!
Always wanted to know, but was afraid to ask :)

Mike Oxford

unread,
Jan 20, 2015, 11:01:33 PM1/20/15
to gremli...@googlegroups.com
Simply do this:
pipe = v1.in('LINKTYPE')[0].name
output = pipe.hasNext() ? pipe.next() : null

Why does this not work?

v1.in('LINKTYPE')[0].name.hasNext() ? pipe.next() : null

Does hasNext() have some kind of syntactic sugar to detect that it's being called off of a null object and automatically return false?

Daniel Kuppitz

unread,
Jan 21, 2015, 6:45:59 AM1/21/15
to gremli...@googlegroups.com
Why does this not work?
v1.in('LINKTYPE')[0].name.hasNext() ? pipe.next() : null

Because you don't even define the pipe object in your snippet.
Anyway, maybe it's worth to mention that you can do this in TP3: v1.in('LINKTYPE').limit(0).values("name").tryNext().orElse(null)

Cheers,
Daniel


Nick De Young

unread,
Jan 21, 2015, 11:11:57 AM1/21/15
to gremli...@googlegroups.com
Trynextorelse 

Looks like scala !!

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages