This is my intermediate level of understanding Cypher, which may be flawed...
The murkiness is that Cypher (and other SQL languages) are declarative (mostly). The FOREACH
on the other hand has more of a procedural flavor... COLLECT
combines things into a LIST
which is then more an entity that has a procedural flavor. The UNWIND
coverts a list of elements back into declarative entities, which rest of Cypher can works.
I suspect the WITH
statements are syntactic sugar that keeps the declarative statements localized yet lets you pass results between the declarative statements.
Consider this Cypher:
(actor:Person)-[:ACTED_IN]->(m:Movie)<-[:ACTED_IN]-(coactor:Person)
There isn't a procedure of "first step", "second step", etc. This is a declarative statement. (Under the hood, the Cypher code does get converted into a set of procedural instructions, which is how computers work... So the declarative nature is a bit of an illusion.)
I think in theory, queries then can be run in parallel without changing anything. (e.g. match actors in parallel and get coactors in parallel.)
Or something like that. I'm still figuring it out.
SQL and Cypher takes a bit of getting used to. It is a peculiar thing.
I hope this helps a little...