diff options
Diffstat (limited to 'doc/docs/de/doc/comprehensions.md')
| -rw-r--r-- | doc/docs/de/doc/comprehensions.md | 271 |
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 | |||
| 3 | Comprehensions 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 | |||
| 7 | The following creates a copy of the items table but with all the values doubled. | ||
| 8 | |||
| 9 | ```yuescript | ||
| 10 | items = [ 1, 2, 3, 4 ] | ||
| 11 | doubled = [item * 2 for i, item in ipairs items] | ||
| 12 | ``` | ||
| 13 | <YueDisplay> | ||
| 14 | |||
| 15 | ```yue | ||
| 16 | items = [ 1, 2, 3, 4 ] | ||
| 17 | doubled = [item * 2 for i, item in ipairs items] | ||
| 18 | ``` | ||
| 19 | |||
| 20 | </YueDisplay> | ||
| 21 | |||
| 22 | The items included in the new table can be restricted with a when clause: | ||
| 23 | |||
| 24 | ```yuescript | ||
| 25 | slice = [item for i, item in ipairs items when i > 1 and i < 3] | ||
| 26 | ``` | ||
| 27 | <YueDisplay> | ||
| 28 | |||
| 29 | ```yue | ||
| 30 | slice = [item for i, item in ipairs items when i > 1 and i < 3] | ||
| 31 | ``` | ||
| 32 | |||
| 33 | </YueDisplay> | ||
| 34 | |||
| 35 | Because 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 | ||
| 38 | doubled = [item * 2 for item in *items] | ||
| 39 | ``` | ||
| 40 | <YueDisplay> | ||
| 41 | |||
| 42 | ```yue | ||
| 43 | doubled = [item * 2 for item in *items] | ||
| 44 | ``` | ||
| 45 | |||
| 46 | </YueDisplay> | ||
| 47 | |||
| 48 | In list comprehensions, you can also use the spread operator `...` to flatten nested lists, achieving a flat map effect: | ||
| 49 | |||
| 50 | ```yuescript | ||
| 51 | data = | ||
| 52 | a: [1, 2, 3] | ||
| 53 | b: [4, 5, 6] | ||
| 54 | |||
| 55 | flat = [...v for k,v in pairs data] | ||
| 56 | -- flat is now [1, 2, 3, 4, 5, 6] | ||
| 57 | ``` | ||
| 58 | <YueDisplay> | ||
| 59 | |||
| 60 | ```yue | ||
| 61 | data = | ||
| 62 | a: [1, 2, 3] | ||
| 63 | b: [4, 5, 6] | ||
| 64 | |||
| 65 | flat = [...v for k,v in pairs data] | ||
| 66 | -- flat is now [1, 2, 3, 4, 5, 6] | ||
| 67 | ``` | ||
| 68 | |||
| 69 | </YueDisplay> | ||
| 70 | |||
| 71 | The 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 | |||
| 73 | Using multiple for clauses is the same as using nested loops: | ||
| 74 | |||
| 75 | ```yuescript | ||
| 76 | x_coords = [4, 5, 6, 7] | ||
| 77 | y_coords = [9, 2, 3] | ||
| 78 | |||
| 79 | points = [ [x, y] for x in *x_coords \ | ||
| 80 | for y in *y_coords] | ||
| 81 | ``` | ||
| 82 | <YueDisplay> | ||
| 83 | |||
| 84 | ```yue | ||
| 85 | x_coords = [4, 5, 6, 7] | ||
| 86 | y_coords = [9, 2, 3] | ||
| 87 | |||
| 88 | points = [ [x, y] for x in *x_coords \ | ||
| 89 | for y in *y_coords] | ||
| 90 | ``` | ||
| 91 | |||
| 92 | </YueDisplay> | ||
| 93 | |||
| 94 | Numeric for loops can also be used in comprehensions: | ||
| 95 | |||
| 96 | ```yuescript | ||
| 97 | evens = [i for i = 1, 100 when i % 2 == 0] | ||
| 98 | ``` | ||
| 99 | <YueDisplay> | ||
| 100 | |||
| 101 | ```yue | ||
| 102 | evens = [i for i = 1, 100 when i % 2 == 0] | ||
| 103 | ``` | ||
| 104 | |||
| 105 | </YueDisplay> | ||
| 106 | |||
| 107 | ## Table Comprehensions | ||
| 108 | |||
| 109 | The syntax for table comprehensions is very similar, only differing by using **{** and **}** and taking two values from each iteration. | ||
| 110 | |||
| 111 | This example makes a copy of the tablething: | ||
| 112 | |||
| 113 | ```yuescript | ||
| 114 | thing = { | ||
| 115 | color: "red" | ||
| 116 | name: "fast" | ||
| 117 | width: 123 | ||
| 118 | } | ||
| 119 | |||
| 120 | thing_copy = {k, v for k, v in pairs thing} | ||
| 121 | ``` | ||
| 122 | <YueDisplay> | ||
| 123 | |||
| 124 | ```yue | ||
| 125 | thing = { | ||
| 126 | color: "red" | ||
| 127 | name: "fast" | ||
| 128 | width: 123 | ||
| 129 | } | ||
| 130 | |||
| 131 | thing_copy = {k, v for k, v in pairs thing} | ||
| 132 | ``` | ||
| 133 | |||
| 134 | </YueDisplay> | ||
| 135 | |||
| 136 | ```yuescript | ||
| 137 | no_color = {k, v for k, v in pairs thing when k != "color"} | ||
| 138 | ``` | ||
| 139 | <YueDisplay> | ||
| 140 | |||
| 141 | ```yue | ||
| 142 | no_color = {k, v for k, v in pairs thing when k != "color"} | ||
| 143 | ``` | ||
| 144 | |||
| 145 | </YueDisplay> | ||
| 146 | |||
| 147 | The **\*** operator is also supported. Here we create a square root look up table for a few numbers. | ||
| 148 | |||
| 149 | ```yuescript | ||
| 150 | numbers = [1, 2, 3, 4] | ||
| 151 | sqrts = {i, math.sqrt i for i in *numbers} | ||
| 152 | ``` | ||
| 153 | <YueDisplay> | ||
| 154 | |||
| 155 | ```yue | ||
| 156 | numbers = [1, 2, 3, 4] | ||
| 157 | sqrts = {i, math.sqrt i for i in *numbers} | ||
| 158 | ``` | ||
| 159 | |||
| 160 | </YueDisplay> | ||
| 161 | |||
| 162 | The 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 | |||
| 164 | In 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 | ||
| 167 | tuples = [ ["hello", "world"], ["foo", "bar"]] | ||
| 168 | tbl = {unpack tuple for tuple in *tuples} | ||
| 169 | ``` | ||
| 170 | <YueDisplay> | ||
| 171 | |||
| 172 | ```yue | ||
| 173 | tuples = [ ["hello", "world"], ["foo", "bar"]] | ||
| 174 | tbl = {unpack tuple for tuple in *tuples} | ||
| 175 | ``` | ||
| 176 | |||
| 177 | </YueDisplay> | ||
| 178 | |||
| 179 | ## Slicing | ||
| 180 | |||
| 181 | A 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 | |||
| 183 | Here we can set the minimum and maximum bounds, taking all items with indexes between 1 and 5 inclusive: | ||
| 184 | |||
| 185 | ```yuescript | ||
| 186 | slice = [item for item in *items[1, 5]] | ||
| 187 | ``` | ||
| 188 | <YueDisplay> | ||
| 189 | |||
| 190 | ```yue | ||
| 191 | slice = [item for item in *items[1, 5]] | ||
| 192 | ``` | ||
| 193 | |||
| 194 | </YueDisplay> | ||
| 195 | |||
| 196 | Any 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 | ||
| 199 | slice = [item for item in *items[2,]] | ||
| 200 | ``` | ||
| 201 | <YueDisplay> | ||
| 202 | |||
| 203 | ```yue | ||
| 204 | slice = [item for item in *items[2,]] | ||
| 205 | ``` | ||
| 206 | |||
| 207 | </YueDisplay> | ||
| 208 | |||
| 209 | If 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 | ||
| 212 | slice = [item for item in *items[,,2]] | ||
| 213 | ``` | ||
| 214 | <YueDisplay> | ||
| 215 | |||
| 216 | ```yue | ||
| 217 | slice = [item for item in *items[,,2]] | ||
| 218 | ``` | ||
| 219 | |||
| 220 | </YueDisplay> | ||
| 221 | |||
| 222 | Both 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 | ||
| 226 | slice = [item for item in *items[-4,-1]] | ||
| 227 | ``` | ||
| 228 | <YueDisplay> | ||
| 229 | |||
| 230 | ```yue | ||
| 231 | -- take the last 4 items | ||
| 232 | slice = [item for item in *items[-4,-1]] | ||
| 233 | ``` | ||
| 234 | |||
| 235 | </YueDisplay> | ||
| 236 | |||
| 237 | The step size can also be negative, which means that the items are taken in reverse order. | ||
| 238 | |||
| 239 | ```yuescript | ||
| 240 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
| 241 | ``` | ||
| 242 | <YueDisplay> | ||
| 243 | |||
| 244 | ```yue | ||
| 245 | reverse_slice = [item for item in *items[-1,1,-1]] | ||
| 246 | ``` | ||
| 247 | |||
| 248 | </YueDisplay> | ||
| 249 | |||
| 250 | ### Slicing Expression | ||
| 251 | |||
| 252 | Slicing 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 | ||
| 256 | sub_list = items[2, 4] | ||
| 257 | |||
| 258 | -- take the last 4 items | ||
| 259 | last_four_items = items[-4, -1] | ||
| 260 | ``` | ||
| 261 | <YueDisplay> | ||
| 262 | |||
| 263 | ```yue | ||
| 264 | -- take the 2nd and 4th items as a new list | ||
| 265 | sub_list = items[2, 4] | ||
| 266 | |||
| 267 | -- take the last 4 items | ||
| 268 | last_four_items = items[-4, -1] | ||
| 269 | ``` | ||
| 270 | |||
| 271 | </YueDisplay> | ||
