Skip to content

Commit c4e7142

Browse files
authored
Merge pull request #1 from mean2me/feature/12-generators
WIP: start translating generators article
2 parents c6d7983 + 7c79c2b commit c4e7142

File tree

1 file changed

+27
-21
lines changed
  • 1-js/12-generators-iterators/1-generators

1 file changed

+27
-21
lines changed

1-js/12-generators-iterators/1-generators/article.md

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# Generators
1+
# I generatori
22

3-
Regular functions return only one, single value (or nothing).
3+
Le funzioni ritornano normalmente un solo valore (a volte non ritornano null).
44

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à.
67

7-
## Generator functions
8+
## Le funzioni generatrici
89

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".
1011

11-
It looks like this:
12+
Ecco un esempio:
1213

1314
```js
1415
function* generateSequence() {
@@ -43,6 +44,7 @@ The function code execution hasn't started yet:
4344
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.
4445

4546
The result of `next()` is always an object with two properties:
47+
4648
- `value`: the yielded value.
4749
- `done`: `true` if the function code has finished, otherwise `false`.
4850

@@ -92,11 +94,12 @@ Now the generator is done. We should see it from `done:true` and process `value:
9294

9395
New calls `generator.next()` don't make sense any more. If we do them, they return the same object: `{done: true}`.
9496

95-
```smart header="`function* f(…)` or `function *f(…)`?"
97+
```smart header="`function* f(…)`or`function *f(…)`?"
9698
Both syntaxes are correct.
9799

98100
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+
````
100103
101104
## Generators are iterable
102105
@@ -116,7 +119,7 @@ let generator = generateSequence();
116119
for(let value of generator) {
117120
alert(value); // 1, then 2
118121
}
119-
```
122+
````
120123

121124
Looks a lot nicer than calling `.next().value`, right?
122125

@@ -183,9 +186,9 @@ let range = {
183186
} else {
184187
return { done: true };
185188
}
186-
}
189+
},
187190
};
188-
}
191+
},
189192
};
190193

191194
// iteration over range returns numbers from range.from to range.to
@@ -201,17 +204,19 @@ let range = {
201204
from: 1,
202205
to: 5,
203206

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++) {
206210
yield value;
207211
}
208-
}
212+
},
209213
};
210214

211-
alert( [...range] ); // 1,2,3,4,5
215+
alert([...range]); // 1,2,3,4,5
212216
```
213217

214218
That works, because `range[Symbol.iterator]()` now returns a generator, and generator methods are exactly what `for..of` expects:
219+
215220
- it has `.next()` method
216221
- that returns values in the form `{value: ..., done: true/false}`
217222

@@ -238,6 +243,7 @@ function* generateSequence(start, end) {
238243
```
239244

240245
Now we'd like to reuse it for generation of a more complex sequence:
246+
241247
- first, digits `0..9` (with character codes 48..57),
242248
- followed by alphabet letters `a..z` (character codes 65..90)
243249
- followed by uppercased letters `A..Z` (character codes 97..122)
@@ -279,7 +285,7 @@ for(let code of generatePasswordCodes()) {
279285
alert(str); // 0..9A..Za..z
280286
```
281287

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.
283289

284290
The result is the same as if we inlined the code from nested generators:
285291

@@ -338,7 +344,7 @@ let generator = gen();
338344

339345
let question = generator.next().value; // <-- yield returns the value
340346

341-
generator.next(4); // --> pass the result into the generator
347+
generator.next(4); // --> pass the result into the generator
342348
```
343349

344350
![](genYield2.svg)
@@ -366,18 +372,18 @@ function* gen() {
366372

367373
alert(ask1); // 4
368374

369-
let ask2 = yield "3 * 3 = ?"
375+
let ask2 = yield "3 * 3 = ?";
370376

371377
alert(ask2); // 9
372378
}
373379

374380
let generator = gen();
375381

376-
alert( generator.next().value ); // "2 + 2 = ?"
382+
alert(generator.next().value); // "2 + 2 = ?"
377383

378-
alert( generator.next(4).value ); // "3 * 3 = ?"
384+
alert(generator.next(4).value); // "3 * 3 = ?"
379385

380-
alert( generator.next(9).done ); // true
386+
alert(generator.next(9).done); // true
381387
```
382388

383389
The execution picture:

0 commit comments

Comments
 (0)