SideEffect a variable, Use it later after BarrierStep?

I seek a query that builds a list and then needs to both sum the list's mapped values and divide the resulting sum by the count of the original list.

This would be the mean() step - if the mapped list was still a Gremlin traversal object that offered mean().

However, the mapped list is, by that time, a Groovy list and mean() is no longer available.

A groovy list can be averaged by reducing it to it sum and then dividing by the count of the list - but to get the count of the list, a separate reference to the list is needed or the count has to have been cached before.

Therefore, is there a way to begin a query, sideEffect a value into a Groovy variable, complete the query, pass through a barrier step, and then divide the saved count?

g.E().
  has('LABEL', 'PROP', 'MYPROPVAL').
  values('createdDate').
  order().by(Order.asc).
// would like to sideEffect cache the count of this set here
  map{ ( LocalDateTime.parse( it.get(), 
                             java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME ).atZone( ZoneId.systemDefault() ).toInstant().toEpochMilli() ) }.
 toList().
 collate(2, true).
 collect{ it -> ( it[1]-it[0] ) / 1000 }.
   inject( 0 ) { accum,it -> accum + it } 
// would like to divide the BigDecimal here by half the count
Solution
It can be done with all Gremlin steps in 3.7.1 where you have date functions available. Assuming:
g.addV().as('a').
  addE('link').from('a').to('a').property('createdDate', '2023-01-01T00:00:00Z').
  addE('link').from('a').to('a').property('createdDate', '2023-01-01T00:30:00Z').
  addE('link').from('a').to('a').property('createdDate', '2023-01-01T01:00:00Z').
  addE('link').from('a').to('a').property('createdDate', '2023-01-01T01:30:00Z').iterate()

You can use the "partition" pattern i came up with for a talk I have a long time ago about DSLs. It effectively does the same as a Groovy collate(). Combining that with the neat new date functions I think this gives you the ability to do the mean() you want at the end:
gremlin> g.E().values('createdDate').asDate().fold().
......1>   emit().
......2>   until(__.not(unfold())).
......3>   repeat(skip(local,2)).
......4>   filter(unfold()).
......5>   limit(local,2).as('dts').limit(local,1).
......6>   dateDiff(select('dts').skip(local,1).unfold()).
......7>   mean()
==>-1800.0

Here's a link to the slides for the talk called "Extending Gremlin with Foundational Steps" in case you're interested: https://www.slideshare.net/StephenMallette/extending-gremlin-with-foundational-steps
SlideShare
Extending Gremlin with Foundational Steps - Download as a PDF or view online for free
Was this page helpful?