title: Tinkerpop Gremlin - How to aggregate variables into traversal independant collections
body: <p>I'm currently reading The Practitioner's Guide to Graph Data and am trying to solve the following problem (just for learning purposes). The following is in the context of the books movie dataset, which in this example makes use of a &quot;Tag&quot; vertex, a &quot;Movie&quot; vertex and a &quot;rated&quot; edge which has a rating property of a value 1-5 .</p>
  <p>Just for practice, and to extend my understanding of concepts from the book, I would like to get all movies tagged with &quot;comedy&quot; and calculate the mean NPS. To do this, I want to aggregate all positive (+1) and neutral or negative (-1) ratings into a list. Then I wish to divide the sum of these values by the amount of variables in this list (the mean). This is what I attempted:</p>
  <pre><code>dev.withSack{[]}{it.clone()}.    // create a sack with an empty list that clones when split
  V().has('Tag', 'tag_name', 'comedy').
      in('topic_tagged').as('film').    // walk to movies tagged as comedy
      inE('rated').    // walk to the rated edges
          choose(values('rating').is(gte(3.0)),
              sack(addAll).by(constant([1.0])),
              sack(addAll).by(constant([-1.0]))).    // add a value or 1 or -1 to this movies list, depending on the rating
      group().
          by(select('film').values('movie_title')).
          by(project('a', 'b').
              by(sack().unfold().sum()).    // add all values from the list
              by(sack().unfold().count()).    // Count the values in the list
              math('a / b')).
      order(local).
          by(values, desc)
  </code></pre>
  <p>This ends up with each movie either being &quot;1.0&quot; or &quot;-1.0&quot;.</p>
  <pre><code>&quot;Journey of August King The (1995)&quot;: &quot;1.0&quot;,
  &quot;Once Upon a Time... When We Were Colored (1995)&quot;: &quot;1.0&quot;, ...
  </code></pre>
  <p>In my testing, it seems the values aren't aggregating into the collection how I expected. I've tried various approaches but none of them achieve my expected result.</p>
  <p>I am aware that I can achieve this result by adding and subtracting from a sack with an initial value of &quot;0.0&quot;, then dividing by the edge count, but I am hoping for a more efficient solution by using a list and avoiding an additional traversal to the edges to get the count.</p>
  <p>Is it possible to achieve my result using a list? If so, how?</p>
  <p>Edit 1:</p>
  <p>The much simpler code below, taken from Kelvins example, will aggregate each rating by simply using the fold step:</p>
  <pre><code>dev.V().
      has('Tag', 'tag_name', 'comedy').
          in('topic_tagged').
          project('movie', 'result').
              by('movie_title').
              by(inE('rated').
                  choose(values('rating').is(gte(3.0)),
                      constant(1.0),
                      constant(-1.0)).
                  fold())    // replace fold() with mean() to calculate the mean, or do something with the collection
  </code></pre>
  <p>I feel a bit embarrassed that I completely forgot about the fold step, as folding and unfolding are so common. Overthinking, I guess.</p>