You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/12-generators-iterators/1-generators/article.md
+27-21Lines changed: 27 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,15 @@
1
-
# Generators
1
+
# I generatori
2
2
3
-
Regular functions return only one, single value (or nothing).
3
+
Le funzioni ritornano normalmente un solo valore (a volte non ritornano null).
4
4
5
-
Generators can return ("yield") multiple values, one after another, on-demand. They work great with [iterables](info:iterable), allowing to create data streams with ease.
5
+
I generatori possono ritornare ("yield") valori multipli, uno dopo l'altro, ogni volta che vengono invocati. Sono, di fatto, lo strumento ideale se usati
6
+
con gli [iterables](info:iterable), dal momento che ci consentono di creare flussi di dati con facilità.
6
7
7
-
## Generator functions
8
+
## Le funzioni generatrici
8
9
9
-
To create a generator, we need a special syntax construct: `function*`, so-called "generator function".
10
+
Per creare un generatore, abbiamo bisogno di uno specifico costrutto sintattico: `function*`, chiamato, appunto, "funzione generatrice".
10
11
11
-
It looks like this:
12
+
Ecco un esempio:
12
13
13
14
```js
14
15
function*generateSequence() {
@@ -43,6 +44,7 @@ The function code execution hasn't started yet:
43
44
The main method of a generator is `next()`. When called, it runs the execution till the nearest `yield <value>` statement (`value` can be omitted, then it's `undefined`). Then the function execution pauses, and the yielded `value` is returned to the outer code.
44
45
45
46
The result of `next()` is always an object with two properties:
47
+
46
48
-`value`: the yielded value.
47
49
-`done`: `true` if the function code has finished, otherwise `false`.
48
50
@@ -92,11 +94,12 @@ Now the generator is done. We should see it from `done:true` and process `value:
92
94
93
95
New calls `generator.next()` don't make sense any more. If we do them, they return the same object: `{done: true}`.
94
96
95
-
```smart header="`function* f(…)` or `function *f(…)`?"
But usually the first syntax is preferred, as the star `*` denotes that it's a generator function, it describes the kind, not the name, so it should stick with the `function` keyword.
99
-
```
101
+
102
+
````
100
103
101
104
## Generators are iterable
102
105
@@ -116,7 +119,7 @@ let generator = generateSequence();
116
119
for(let value of generator) {
117
120
alert(value); // 1, then 2
118
121
}
119
-
```
122
+
````
120
123
121
124
Looks a lot nicer than calling `.next().value`, right?
122
125
@@ -183,9 +186,9 @@ let range = {
183
186
} else {
184
187
return { done:true };
185
188
}
186
-
}
189
+
},
187
190
};
188
-
}
191
+
},
189
192
};
190
193
191
194
// iteration over range returns numbers from range.from to range.to
@@ -201,17 +204,19 @@ let range = {
201
204
from:1,
202
205
to:5,
203
206
204
-
*[Symbol.iterator]() { // a shorthand for [Symbol.iterator]: function*()
205
-
for(let value =this.from; value <=this.to; value++) {
207
+
*[Symbol.iterator]() {
208
+
// a shorthand for [Symbol.iterator]: function*()
209
+
for (let value =this.from; value <=this.to; value++) {
206
210
yield value;
207
211
}
208
-
}
212
+
},
209
213
};
210
214
211
-
alert([...range]); // 1,2,3,4,5
215
+
alert([...range]); // 1,2,3,4,5
212
216
```
213
217
214
218
That works, because `range[Symbol.iterator]()` now returns a generator, and generator methods are exactly what `for..of` expects:
219
+
215
220
- it has `.next()` method
216
221
- that returns values in the form `{value: ..., done: true/false}`
Now we'd like to reuse it for generation of a more complex sequence:
246
+
241
247
- first, digits `0..9` (with character codes 48..57),
242
248
- followed by alphabet letters `a..z` (character codes 65..90)
243
249
- followed by uppercased letters `A..Z` (character codes 97..122)
@@ -279,7 +285,7 @@ for(let code of generatePasswordCodes()) {
279
285
alert(str); // 0..9A..Za..z
280
286
```
281
287
282
-
The `yield*` directive *delegates* the execution to another generator. This term means that `yield* gen` iterates over the generator `gen` and transparently forwards its yields outside. As if the values were yielded by the outer generator.
288
+
The `yield*` directive _delegates_ the execution to another generator. This term means that `yield* gen` iterates over the generator `gen` and transparently forwards its yields outside. As if the values were yielded by the outer generator.
283
289
284
290
The result is the same as if we inlined the code from nested generators:
285
291
@@ -338,7 +344,7 @@ let generator = gen();
338
344
339
345
let question =generator.next().value; // <-- yield returns the value
340
346
341
-
generator.next(4); // --> pass the result into the generator
347
+
generator.next(4); // --> pass the result into the generator
0 commit comments