Skip to content

Commit eea7358

Browse files
post review corrections 1
1 parent 0b1d7f1 commit eea7358

File tree

1 file changed

+190
-101
lines changed

1 file changed

+190
-101
lines changed

modules/ROOT/pages/expressions/list-expressions.adoc

Lines changed: 190 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -32,105 +32,6 @@ CREATE (alice:Person {name:'Alice', age: 65, role: 'Project manager', skills: ['
3232
(daniel)-[:WORKS_FOR]->(eskil)
3333
----
3434

35-
[[list-concatenation]]
36-
== List concatenation
37-
38-
Cypher contains two list concatenation operators: `||` and `+`.
39-
`||` is xref:appendix/gql-conformance/index.adoc[GQL conformant], while `+` is not.
40-
41-
.List concatenation using `||` and `+`
42-
[source, cypher]
43-
----
44-
RETURN [1,2] || [3,4] AS list1,
45-
[1,2] + [3,4] AS list2
46-
----
47-
48-
.Result
49-
[role="queryresult",options="header,footer",cols="2*<m"]
50-
|===
51-
| list1 | list2
52-
53-
| [1, 2, 3, 4] | [1, 2, 3, 4]
54-
55-
2+d|Rows: 1
56-
|===
57-
58-
.Concatenate two `LIST` properties
59-
[source, cypher]
60-
----
61-
MATCH (cecil:Person {name: 'Cecil'}), (cecilia:Person {name: 'Cecilia'})
62-
RETURN cecil.skills || cecilia.skills AS combinedSkills
63-
----
64-
65-
.Result
66-
[role="queryresult",options="header,footer",cols="1*<m"]
67-
|===
68-
| combinedSkills
69-
70-
| ["Java", "Python", "JavaScript", "TypeScript"]
71-
72-
1+d|Rows: 1
73-
|===
74-
75-
The `+` operator can add elements to the beginning or end of a `LIST` value.
76-
This is not possible using the `||` operator.
77-
78-
.Add elements to the beginning and end of a `LIST`
79-
[source, cypher]
80-
----
81-
WITH [1, 2, 3, 4] AS list
82-
RETURN 0 + list AS newBeginning,
83-
list + 5 AS newEnd
84-
----
85-
86-
87-
.Result
88-
[role="queryresult",options="header,footer",cols="2*<m"]
89-
|===
90-
| newBeginning | newEnd
91-
92-
| [0, 1, 2, 3, 4] | [1, 2, 3, 4, 5]
93-
94-
2+d|Rows: 1
95-
|===
96-
97-
.Add elements to the beginning of a `LIST` property
98-
[source, cypher]
99-
----
100-
MATCH (cecil:Person {name: 'Cecil'})
101-
SET cecil.skills = "Cypher" + cecil.skills
102-
RETURN cecil.skills AS skillsList
103-
----
104-
105-
.Result
106-
[role="queryresult",options="header,footer",cols="1*<m"]
107-
|===
108-
| skillsList
109-
110-
| ["Cypher", "Java", "Python"]
111-
112-
1+d|Rows: 1
113-
|===
114-
115-
If `NULL` is part of a concatenated `LIST`, `NULL` will be a part of the new `LIST`.
116-
117-
.Concatenate `LIST` including `NULL`
118-
[source, cypher]
119-
----
120-
RETURN [1, 2] || [3, null] AS listWithNull
121-
----
122-
123-
.Result
124-
[role="queryresult",options="header,footer",cols="1*<m"]
125-
|===
126-
| listWithNull
127-
128-
| [1, 2, 3, NULL]
129-
130-
1+d|Rows: 1
131-
|===
132-
133-
For removing `NULL` values when concatenating `LIST` values, see xref:expressions/list-expressions.adoc#null-list-concatenation-list-comprehension[`NULL`, list concatenation, and list comprehension].
13435

13536
[[list-element-access]]
13637
== List element access
@@ -202,6 +103,44 @@ RETURN nestedList[1] AS secondList
202103
1+d|Rows: 1
203104
|===
204105

106+
.Access specific elements in a nested `LIST`
107+
[source, cypher]
108+
----
109+
WITH [[1, 2], [3, 4], [5, 6]] AS nestedList
110+
RETURN nestedList[1] AS secondList,
111+
nestedList[1][0] AS firstElementOfSecondList
112+
----
113+
114+
.Result
115+
[role="queryresult",options="header,footer",cols="2*<m"]
116+
|===
117+
| secondList | firstElementOfSecondList
118+
119+
| [3, 4] | 3
120+
121+
2+d|Rows: 1
122+
|===
123+
124+
The index in the list element access can be any expression, including a variable.
125+
126+
.Access list elements using a variable as an index
127+
[source, cypher]
128+
----
129+
WITH [[1, 2], [3, 4], [5, 6]] AS nestedList, 2 AS listIndex
130+
RETURN nestedList[listIndex] AS thirdList,
131+
nestedList[listIndex][listIndex - 1] AS secondElementOfThirdList
132+
----
133+
134+
.Result
135+
[role="queryresult",options="header,footer",cols="2*<m"]
136+
|===
137+
| thirdList | secondElementOfThirdList
138+
139+
| [5, 6] | 6
140+
141+
2+d|Rows: 1
142+
|===
143+
205144
The xref:expressions/predicates/list-operators.adoc[`IN`] operator, which checks for `LIST` membership, can be used together with `[]` to test whether an element exists in a nested `LIST`.
206145

207146
.Check for membership in a nested `LIST`
@@ -221,6 +160,7 @@ RETURN 3 IN nestedList[0] AS elementPresent
221160
1+d|Rows: 1
222161
|===
223162

163+
224164
Attempting to reference an element outside the bounds of the `LIST` will return `NULL`, as will attempting to access elements from an empty `LIST`.
225165

226166
.Out-of-bounds and empty `LIST` access
@@ -325,10 +265,138 @@ RETURN list[0] + [2] + list[1..] AS newList
325265
1+d|Rows: 1
326266
|===
327267

268+
[[list-concatenation]]
269+
== List concatenation
270+
271+
Cypher contains two list concatenation operators: `||` and `+`.
272+
`||` is xref:appendix/gql-conformance/index.adoc[GQL conformant], while `+` is not.
273+
274+
.List concatenation using `||` and `+`
275+
[source, cypher]
276+
----
277+
RETURN [1,2] || [3,4] AS list1,
278+
[1,2] + [3,4] AS list2
279+
----
280+
281+
.Result
282+
[role="queryresult",options="header,footer",cols="2*<m"]
283+
|===
284+
| list1 | list2
285+
286+
| [1, 2, 3, 4] | [1, 2, 3, 4]
287+
288+
2+d|Rows: 1
289+
|===
290+
291+
.Concatenate two `LIST` properties
292+
[source, cypher]
293+
----
294+
MATCH (cecil:Person {name: 'Cecil'}), (cecilia:Person {name: 'Cecilia'})
295+
RETURN cecil.skills || cecilia.skills AS combinedSkills
296+
----
297+
298+
.Result
299+
[role="queryresult",options="header,footer",cols="1*<m"]
300+
|===
301+
| combinedSkills
302+
303+
| ["Java", "Python", "JavaScript", "TypeScript"]
304+
305+
1+d|Rows: 1
306+
|===
307+
308+
If `NULL` is part of a concatenated `LIST`, `NULL` will be a part of the new `LIST`.
309+
310+
.Concatenate `LIST` including `NULL`
311+
[source, cypher]
312+
----
313+
RETURN [1, 2] || [3, null] AS listWithNull
314+
----
315+
316+
.Result
317+
[role="queryresult",options="header,footer",cols="1*<m"]
318+
|===
319+
| listWithNull
320+
321+
| [1, 2, 3, NULL]
322+
323+
1+d|Rows: 1
324+
|===
325+
326+
For removing `NULL` values when concatenating `LIST` values, see xref:expressions/list-expressions.adoc#null-list-concatenation-list-comprehension[`NULL`, list concatenation, and list comprehension].
327+
328+
[[add-elements]]
329+
=== Add elements to a list
330+
331+
The `+` operator can add elements to the beginning or end of a `LIST` value.
332+
This is not possible using the `||` operator.
333+
334+
.Add elements to the beginning and end of a `LIST`
335+
[source, cypher]
336+
----
337+
WITH [1, 2, 3, 4] AS list
338+
RETURN 0 + list AS newBeginning,
339+
list + 5 AS newEnd
340+
----
341+
342+
.Result
343+
[role="queryresult",options="header,footer",cols="2*<m"]
344+
|===
345+
| newBeginning | newEnd
346+
347+
| [0, 1, 2, 3, 4] | [1, 2, 3, 4, 5]
348+
349+
2+d|Rows: 1
350+
|===
351+
352+
To insert a `LIST` value into a nested `LIST`, the added `LIST` must itself be nested.
353+
If the added `LIST` is not nested, its elements are treated as individual elements, whereas if it is nested, it maintains the `LIST` structure within the nested `LIST`.
354+
355+
.Add `LIST` values to a nested `LIST`
356+
[source, cypher]
357+
----
358+
WITH [[1, 2], [3, 4]] AS nestedList
359+
RETURN nestedList + [5, 6] AS nonNestedAddition
360+
nestedList + [[5, 6]] AS nestedAddition
361+
----
362+
363+
.Result
364+
[role="queryresult",options="header,footer",cols="2*<m"]
365+
|===
366+
| nonNestedAddition | nestedAddition
367+
368+
| [[1, 2], [3, 4], 5, 6] | [[1, 2], [3, 4], [5, 6]]
369+
370+
2+d|Rows: 1
371+
|===
372+
373+
.Add elements to the beginning of a `LIST` property
374+
[source, cypher]
375+
----
376+
MATCH (cecil:Person {name: 'Cecil'})
377+
SET cecil.skills = "Cypher" + cecil.skills
378+
RETURN cecil.skills AS skillsList
379+
----
380+
381+
.Result
382+
[role="queryresult",options="header,footer",cols="1*<m"]
383+
|===
384+
| skillsList
385+
386+
| ["Cypher", "Java", "Python"]
387+
388+
1+d|Rows: 1
389+
|===
390+
391+
392+
393+
328394
[[list-comprehension]]
329395
== List comprehension
330396

331397
List comprehension is used to create new `LIST` values by iterating over existing `LIST` values and transforming the elements based on certain conditions or operations.
398+
This process effectively maps each element in the original `LIST` to a new value.
399+
The result is a new `LIST` that consists of the transformed elements.
332400

333401
.Syntax
334402
[source, syntax]
@@ -388,7 +456,28 @@ RETURN [n IN list WHERE n > 2 | n] AS filteredList
388456
1+d|Rows: 1
389457
|===
390458

391-
This below query iterates over the `skills` property of each Person node and creates a new `LIST` by xref:expressions/string-operators.adoc[concatenating the `STRING`] `" expert"` to each element in `skills`.
459+
The next example shows how to map a `LIST` using its indexes with a list comprehension.
460+
The xref:functions/list.adoc#functions-range[`range()`] function is used to generate the indexes from `0` to the last valid index of the `LIST`, and then each index is combined with its corresponding `LIST` value into a `STRING` value.
461+
The result is a `LIST` of `STRING` values formatted as `'index: value'`.
462+
463+
.Map list elements using indexes
464+
[source, cypher]
465+
----
466+
WITH [1,2,3,4] AS list
467+
RETURN [listIndex IN range(0, size(list)-1) | toString(listIndex) || ': ' || toString(list[listIndex])] AS mappedListElements
468+
----
469+
470+
.Result
471+
[role="queryresult",options="header,footer",cols="1*<m"]
472+
|===
473+
| mappedListElements
474+
475+
| ["0: 1", "1: 2", "2: 3", "3: 4"]
476+
477+
1+d|Rows: 1
478+
|===
479+
480+
The below query iterates over the `skills` property of each `Person` node and creates a new `LIST` by xref:expressions/string-operators.adoc[concatenating the `STRING`] `" expert"` to each element in `skills`.
392481

393482
.Modify `LIST` properties using list comprehension
394483
[source, cypher]
@@ -423,7 +512,6 @@ MATCH (p:Person)
423512
RETURN [person IN collect(p) WHERE 'Python' IN person.skills | person.name] AS pythonExperts
424513
----
425514

426-
427515
.Result
428516
[role="queryresult",options="header,footer",cols="1*<m"]
429517
|===
@@ -535,3 +623,4 @@ RETURN collect(DISTINCT superiorsSkills) AS distinctSuperiorsSkills
535623
1+d|Rows: 1
536624
|===
537625

626+

0 commit comments

Comments
 (0)