Conditionally updating a variable with choose()

How do I create and update a variable with a conditional? I need a number to be calculated based on an arrangement of requirements. Here is a very simplified example of what I was trying to do: g.addV('App').property('name', 'A').property('requirement1', true).property('requirement2', true). addV('App').property('name', 'B').property('requirement1', true).property('requirement2', false). addV('App').property('name', 'C').property('requirement1', false).property('requirement2', false).property('requirement3', true) g.V(). project('id', 'name', 'requirement1', 'requirement2', 'value'). by('id'). by('name'). by('requirement1'). by('requirement2'). by( map{ total = 0; choose( and( has('requirement1', true), has('requirement2', true)), constant(total + 1), identity() ) choose( has('requirement3', true), constant(total + 1), identity() ) total } ) I ultimately found a solution, but I ended up having to create different conditionals for every possible combination. It is repetitive and hard to read. Is there a way to do this, that is similar to what I shared above? Thank you!
Solution:
sorry, but it looks like this question was missed last week somehow. i think you're mostly on track for the best way to do this by using choose(). you could simplify your syntax a fair bit i think by using sack(): ```gremlin> g.withSack(0).V(). ......1> project('id', 'name', 'requirement1', 'requirement2', 'requirement3', 'value'). ......2> by('id'). ......3> by('name')....
Jump to solution
2 Replies
Solution
spmallette
spmallette9mo ago
sorry, but it looks like this question was missed last week somehow. i think you're mostly on track for the best way to do this by using choose(). you could simplify your syntax a fair bit i think by using sack():
gremlin> g.withSack(0).V().
......1> project('id', 'name', 'requirement1', 'requirement2', 'requirement3', 'value').
......2> by('id').
......3> by('name').
......4> by('requirement1').
......5> by('requirement2').
......6> by('requirement3').
......7> by(choose(has('requirement1', true).has('requirement2', true), sack(sum).by(constant(1))).
......8> choose(has('requirement3', true), sack(sum).by(constant(1))).
......9> sack())
==>[name:A,requirement1:true,requirement2:true,value:1]
==>[name:B,requirement1:true,requirement2:false,value:0]
==>[name:C,requirement1:false,requirement2:false,requirement3:true,value:1]
gremlin> g.withSack(0).V().
......1> project('id', 'name', 'requirement1', 'requirement2', 'requirement3', 'value').
......2> by('id').
......3> by('name').
......4> by('requirement1').
......5> by('requirement2').
......6> by('requirement3').
......7> by(choose(has('requirement1', true).has('requirement2', true), sack(sum).by(constant(1))).
......8> choose(has('requirement3', true), sack(sum).by(constant(1))).
......9> sack())
==>[name:A,requirement1:true,requirement2:true,value:1]
==>[name:B,requirement1:true,requirement2:false,value:0]
==>[name:C,requirement1:false,requirement2:false,requirement3:true,value:1]
I'm not sure how complex your conditionals will end up being but with some proper indentation i think it can stay readable.
Katlin
KatlinOP9mo ago
Thank you, this is a much better solution!
Want results from more Discord servers?
Add your server