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 list comprehension #1145

Open
wants to merge 2 commits into
base: memgraph-3-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions pages/querying/differences-in-cypher-implementations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,6 @@ In other cases, Memgraph does not yet support patterns in functions, e.g. `size(
Most of the time, the same functionalities can be expressed differently in Memgraph
using `OPTIONAL` expansions, function calls, etc.

{<h4 className="custom-header">List comprehension</h4>}
The following type of query is not yet supported in Memgraph:
```cypher
MATCH (keanu:Person {name:'Keanu Reeves'})
RETURN [x IN keanu.resume WHERE x contains 'The Matrix'] AS matrixList
```

### Unsupported expressions

{<h4 className="custom-header">Cypher expressions</h4>}
Expand Down
60 changes: 60 additions & 0 deletions pages/querying/expressions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,63 @@ WITH keanu,[(keanu)-->(b:Movie) | b.title] + [(keanu)-->(b:Movie) | b.released]
SET keanu.resume = movieTitles
RETURN keanu.resume
```

## List comprehension

List comprehension is a syntactic construct in Cypher that allows for the creation of a new list by evaluating an expression over each element of an existing list, optionally filtering elements based on a predicate. This feature is particularly useful for transforming and filtering data within queries.

The general syntax for list comprehension in Cypher is:

```cypher
[variable IN list [WHERE predicate] | expression]
```

- **`variable`**: Represents each element in the original list.
- **`list`**: The original list to iterate over.
- **`predicate`** (optional): A condition that filters elements; only elements satisfying this condition are processed.
- **`expression`**: An expression applied to each filtered element; the results form the new list.

**Examples:**

1. **Transforming a List:**

To create a list of squares from a list of numbers:

```cypher
RETURN [x IN [1, 2, 3, 4] | x * x] AS squares
```

**Result:** `[1, 4, 9, 16]`

2. **Filtering a List:**

To filter out even numbers from a list:

```cypher
RETURN [x IN [1, 2, 3, 4] WHERE x % 2 <> 0] AS oddNumbers
```

**Result:** `[1, 3]`

3. **Combining Transformation and Filtering:**

To create a list of squares of even numbers:

```cypher
RETURN [x IN [1, 2, 3, 4] WHERE x % 2 = 0 | x * x] AS evenSquares
```

**Result:** `[4, 16]`

4. **Extracting Node Properties:**

Assuming a graph where `Person` nodes are connected to `Movie` nodes with an `ACTED_IN` relationship, to retrieve the titles of movies released after the year 2000 that a person named 'Alice' acted in:

```cypher
MATCH (alice:Person {name: 'Alice'})-[:ACTED_IN]->(movie:Movie)
RETURN [m IN collect(movie) WHERE m.released > 2000 | m.title] AS recentMovies
```

**Result:** A list of movie titles released after 2000 that 'Alice' acted in.

List comprehensions enhance the expressiveness of Cypher queries by enabling concise data transformation and filtering directly within the query syntax.