Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of how to use multiple values in a sack #239

Open
krlawrence opened this issue Apr 20, 2022 · 1 comment
Open

Add example of how to use multiple values in a sack #239

krlawrence opened this issue Apr 20, 2022 · 1 comment

Comments

@krlawrence
Copy link
Owner

Gremlin currently does not provide a way for a sack to contain multiple values. You can do it using a map, but it gets a bit messy. It would be good to have an example in the book that shows how to work around this. Such as, given the following graph:

g.addV('stop').property(id,'s1').as('s1').
  addV('stop').property(id,'s2').as('s2').
  addV('stop').property(id,'s3').as('s3').  
  addV('stop').property(id,'s4').as('s4').  
  addV('stop').property(id,'s5').as('s5').  
  addV('stop').property(id,'s6').as('s6').  
  addV('stop').property(id,'s7').as('s7').
  addE('link').from('s1').to('s2').property('p1',1).property('p2',2).
  addE('link').from('s2').to('s3').property('p1',3).property('p2',4).
  addE('link').from('s3').to('s4').property('p1',5).property('p2',6).
  addE('link').from('s4').to('s5').property('p1',7).property('p2',8).
  addE('link').from('s5').to('s6').property('p1',9).property('p2',10).
  addE('link').from('s6').to('s7').property('p1',11).property('p2',12)

We could work with two sets of edge properties as follows

g.withSack(['a':0,'b':0]).
  V('s1').
  repeat(
    outE().as('x').
    sack(assign).
      by(project('a','b').
        by(sack().math('a + _').by().by(select('x').values('p1'))).
        by(sack().math('b + _').by().by(select('x').values('p2')))).
    inV()).
    until(not(out())).
  sack()

Which produces

{'a': 36.0, 'b': 42.0}
@krlawrence krlawrence added this to New issues in Planning via automation Apr 20, 2022
@krlawrence krlawrence moved this from New issues to Medium Priority in Planning May 4, 2022
@krlawrence
Copy link
Owner Author

Other examples could include complex list manipulation such as

gremlin> g.V(1,2,3).values('city').fold().sack(assign)
==>[Atlanta,Anchorage,Austin]

gremlin> g.V(1,2,3).values('city').fold().sack(assign).sack()
==>[Atlanta,Anchorage,Austin]

gremlin> g.V(1,2,3).values('city').fold().sack(assign).V(4).union(values('city'),sack().unfold()).fold().sac
k(assign).sack()
==>[Nashville,Atlanta,Anchorage,Austin]      

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Planning
  
Medium Priority
Development

No branches or pull requests

1 participant