neo4j/community — pull request #884 by wfreeman (2012-09-28 02:22:03+00:00)
Title: Cypher: Add Reduce functionality (expressions applied across a collection and stored in an accumulator)

I think I did this right this time. With the docgen test and the basic unit test, modeled after Extract. Also, I put it in a branch so it's easier to work on other things while it's waiting to be merged in.

In any case, this functionality has been discussed pretty thoroughly in my previous pull request:
https://github.com/neo4j/community/pull/878

But I'll repeat some basics here again. The goal is to be able to do operations against collections so you can do things like add up totals of a numeric field, or get the aggregate difference between two fields. It's pretty powerful. Here's an example:

```
start n=node(*) return n;
+-------------------------------------+
| n |
+-------------------------------------+
| Node[7]{min:1,max:9} |
| Node[8]{min:3,max:8} |
| Node[9]{min:10,max:20} |
+-------------------------------------+
start n=node(7,8,9)
with collect(n) as nodes
return
reduce(acc = [], x in nodes: acc + [[x.max,x.min]]) as arrayMaxMin,
reduce(acc = 0,x in nodes: acc + (x.max - x.min)) as maxMinusMin;
+-------------------------------------+
| arrayMaxMin | maxMinusMin |
+-------------------------------------+
| [[9,1],[8,3],[20,10]] | 23.0 |
+-------------------------------------+
```
Let me know if you need anything else.