aboutsummaryrefslogtreecommitdiff
path: root/doc/docs/de/doc/comprehensions.md
diff options
context:
space:
mode:
Diffstat (limited to 'doc/docs/de/doc/comprehensions.md')
-rw-r--r--doc/docs/de/doc/comprehensions.md271
1 files changed, 271 insertions, 0 deletions
diff --git a/doc/docs/de/doc/comprehensions.md b/doc/docs/de/doc/comprehensions.md
new file mode 100644
index 0000000..3a92167
--- /dev/null
+++ b/doc/docs/de/doc/comprehensions.md
@@ -0,0 +1,271 @@
1# Comprehensions
2
3Comprehensions provide a convenient syntax for constructing a new table by iterating over some existing object and applying an expression to its values. There are two kinds of comprehensions: list comprehensions and table comprehensions. They both produce Lua tables; list comprehensions accumulate values into an array-like table, and table comprehensions let you set both the key and the value on each iteration.
4
5## List Comprehensions
6
7The following creates a copy of the items table but with all the values doubled.
8
9```yuescript
10items = [ 1, 2, 3, 4 ]
11doubled = [item * 2 for i, item in ipairs items]
12```
13<YueDisplay>
14
15```yue
16items = [ 1, 2, 3, 4 ]
17doubled = [item * 2 for i, item in ipairs items]
18```
19
20</YueDisplay>
21
22The items included in the new table can be restricted with a when clause:
23
24```yuescript
25slice = [item for i, item in ipairs items when i > 1 and i < 3]
26```
27<YueDisplay>
28
29```yue
30slice = [item for i, item in ipairs items when i > 1 and i < 3]
31```
32
33</YueDisplay>
34
35Because it is common to iterate over the values of a numerically indexed table, an **\*** operator is introduced. The doubled example can be rewritten as:
36
37```yuescript
38doubled = [item * 2 for item in *items]
39```
40<YueDisplay>
41
42```yue
43doubled = [item * 2 for item in *items]
44```
45
46</YueDisplay>
47
48In list comprehensions, you can also use the spread operator `...` to flatten nested lists, achieving a flat map effect:
49
50```yuescript
51data =
52 a: [1, 2, 3]
53 b: [4, 5, 6]
54
55flat = [...v for k,v in pairs data]
56-- flat is now [1, 2, 3, 4, 5, 6]
57```
58<YueDisplay>
59
60```yue
61data =
62 a: [1, 2, 3]
63 b: [4, 5, 6]
64
65flat = [...v for k,v in pairs data]
66-- flat is now [1, 2, 3, 4, 5, 6]
67```
68
69</YueDisplay>
70
71The for and when clauses can be chained as much as desired. The only requirement is that a comprehension has at least one for clause.
72
73Using multiple for clauses is the same as using nested loops:
74
75```yuescript
76x_coords = [4, 5, 6, 7]
77y_coords = [9, 2, 3]
78
79points = [ [x, y] for x in *x_coords \
80for y in *y_coords]
81```
82<YueDisplay>
83
84```yue
85x_coords = [4, 5, 6, 7]
86y_coords = [9, 2, 3]
87
88points = [ [x, y] for x in *x_coords \
89for y in *y_coords]
90```
91
92</YueDisplay>
93
94Numeric for loops can also be used in comprehensions:
95
96```yuescript
97evens = [i for i = 1, 100 when i % 2 == 0]
98```
99<YueDisplay>
100
101```yue
102evens = [i for i = 1, 100 when i % 2 == 0]
103```
104
105</YueDisplay>
106
107## Table Comprehensions
108
109The syntax for table comprehensions is very similar, only differing by using **{** and **}** and taking two values from each iteration.
110
111This example makes a copy of the tablething:
112
113```yuescript
114thing = {
115 color: "red"
116 name: "fast"
117 width: 123
118}
119
120thing_copy = {k, v for k, v in pairs thing}
121```
122<YueDisplay>
123
124```yue
125thing = {
126 color: "red"
127 name: "fast"
128 width: 123
129}
130
131thing_copy = {k, v for k, v in pairs thing}
132```
133
134</YueDisplay>
135
136```yuescript
137no_color = {k, v for k, v in pairs thing when k != "color"}
138```
139<YueDisplay>
140
141```yue
142no_color = {k, v for k, v in pairs thing when k != "color"}
143```
144
145</YueDisplay>
146
147The **\*** operator is also supported. Here we create a square root look up table for a few numbers.
148
149```yuescript
150numbers = [1, 2, 3, 4]
151sqrts = {i, math.sqrt i for i in *numbers}
152```
153<YueDisplay>
154
155```yue
156numbers = [1, 2, 3, 4]
157sqrts = {i, math.sqrt i for i in *numbers}
158```
159
160</YueDisplay>
161
162The key-value tuple in a table comprehension can also come from a single expression, in which case the expression should return two values. The first is used as the key and the second is used as the value:
163
164In this example we convert an array of pairs to a table where the first item in the pair is the key and the second is the value.
165
166```yuescript
167tuples = [ ["hello", "world"], ["foo", "bar"]]
168tbl = {unpack tuple for tuple in *tuples}
169```
170<YueDisplay>
171
172```yue
173tuples = [ ["hello", "world"], ["foo", "bar"]]
174tbl = {unpack tuple for tuple in *tuples}
175```
176
177</YueDisplay>
178
179## Slicing
180
181A special syntax is provided to restrict the items that are iterated over when using the **\*** operator. This is equivalent to setting the iteration bounds and a step size in a for loop.
182
183Here we can set the minimum and maximum bounds, taking all items with indexes between 1 and 5 inclusive:
184
185```yuescript
186slice = [item for item in *items[1, 5]]
187```
188<YueDisplay>
189
190```yue
191slice = [item for item in *items[1, 5]]
192```
193
194</YueDisplay>
195
196Any of the slice arguments can be left off to use a sensible default. In this example, if the max index is left off it defaults to the length of the table. This will take everything but the first element:
197
198```yuescript
199slice = [item for item in *items[2,]]
200```
201<YueDisplay>
202
203```yue
204slice = [item for item in *items[2,]]
205```
206
207</YueDisplay>
208
209If the minimum bound is left out, it defaults to 1. Here we only provide a step size and leave the other bounds blank. This takes all odd indexed items: (1, 3, 5, …)
210
211```yuescript
212slice = [item for item in *items[,,2]]
213```
214<YueDisplay>
215
216```yue
217slice = [item for item in *items[,,2]]
218```
219
220</YueDisplay>
221
222Both the minimum and maximum bounds can be negative, which means that the bounds are counted from the end of the table.
223
224```yuescript
225-- take the last 4 items
226slice = [item for item in *items[-4,-1]]
227```
228<YueDisplay>
229
230```yue
231-- take the last 4 items
232slice = [item for item in *items[-4,-1]]
233```
234
235</YueDisplay>
236
237The step size can also be negative, which means that the items are taken in reverse order.
238
239```yuescript
240reverse_slice = [item for item in *items[-1,1,-1]]
241```
242<YueDisplay>
243
244```yue
245reverse_slice = [item for item in *items[-1,1,-1]]
246```
247
248</YueDisplay>
249
250### Slicing Expression
251
252Slicing can also be used as an expression. This is useful for getting a sub-list of a table.
253
254```yuescript
255-- take the 2nd and 4th items as a new list
256sub_list = items[2, 4]
257
258-- take the last 4 items
259last_four_items = items[-4, -1]
260```
261<YueDisplay>
262
263```yue
264-- take the 2nd and 4th items as a new list
265sub_list = items[2, 4]
266
267-- take the last 4 items
268last_four_items = items[-4, -1]
269```
270
271</YueDisplay>