functionalscript 0.0.349 → 0.0.350
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LANGUAGE.md +40 -0
- package/package.json +1 -1
package/LANGUAGE.md
CHANGED
|
@@ -206,3 +206,43 @@ const f = () => x // < invalid
|
|
|
206
206
|
### 9.7. Block
|
|
207
207
|
|
|
208
208
|
[Block](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block)
|
|
209
|
+
|
|
210
|
+
## 10. Generators
|
|
211
|
+
|
|
212
|
+
For compatibility reason, FunctionalScript allows to create generators as implementation of `[Symbol.iterator]` function. However, it doesn't allow to read the `[Symbol.iterator]` property. For example
|
|
213
|
+
|
|
214
|
+
This code is allowed
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
/** @type {<T>(list: List<T>) => Iterable<T>} */
|
|
218
|
+
const iterable = list => ({
|
|
219
|
+
*[Symbol.iterator]() {
|
|
220
|
+
let i = list
|
|
221
|
+
while (true) {
|
|
222
|
+
const r = next(i)
|
|
223
|
+
if (r === undefined) { return }
|
|
224
|
+
yield r.first
|
|
225
|
+
i = r.tail
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
})
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
The following code is not allowed, because `iterator` is a mutated object by design in JavaScript.
|
|
232
|
+
|
|
233
|
+
```js
|
|
234
|
+
const it = [0, 1, 2][Symbol.iterator] //< compilation error.
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Use `Iterable` instead of `Iterator`.
|
|
238
|
+
|
|
239
|
+
```js
|
|
240
|
+
const x = () => {
|
|
241
|
+
const a = [0, 1, 2] // iterable
|
|
242
|
+
let sum = 0;
|
|
243
|
+
for (let i in a) {
|
|
244
|
+
sum = sum + i
|
|
245
|
+
}
|
|
246
|
+
return sum;
|
|
247
|
+
}
|
|
248
|
+
```
|