firefly-compiler 0.4.51 → 0.4.52

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.
Files changed (109) hide show
  1. package/.hintrc +4 -4
  2. package/.vscode/settings.json +4 -4
  3. package/bin/Release.ff +153 -153
  4. package/bin/firefly.mjs +1 -1
  5. package/compiler/Builder.ff +257 -257
  6. package/compiler/Compiler.ff +227 -227
  7. package/compiler/Dependencies.ff +186 -186
  8. package/compiler/DependencyLock.ff +17 -17
  9. package/compiler/JsEmitter.ff +946 -946
  10. package/compiler/LspHook.ff +202 -202
  11. package/compiler/ModuleCache.ff +178 -178
  12. package/compiler/Workspace.ff +88 -88
  13. package/core/.firefly/include/package-lock.json +394 -394
  14. package/core/.firefly/include/package.json +5 -5
  15. package/core/.firefly/include/prepare.sh +1 -1
  16. package/core/.firefly/package.ff +2 -2
  17. package/core/Array.ff +265 -265
  18. package/core/Atomic.ff +64 -64
  19. package/core/Box.ff +7 -7
  20. package/core/BrowserSystem.ff +40 -40
  21. package/core/BuildSystem.ff +148 -148
  22. package/core/Crypto.ff +96 -96
  23. package/core/Equal.ff +36 -36
  24. package/core/HttpClient.ff +87 -87
  25. package/core/JsSystem.ff +69 -69
  26. package/core/Json.ff +434 -434
  27. package/core/List.ff +486 -486
  28. package/core/Lock.ff +144 -144
  29. package/core/NodeSystem.ff +195 -189
  30. package/core/Ordering.ff +161 -161
  31. package/core/Path.ff +401 -401
  32. package/core/Random.ff +134 -134
  33. package/core/RbMap.ff +216 -216
  34. package/core/Show.ff +43 -43
  35. package/core/SourceLocation.ff +68 -68
  36. package/core/Task.ff +141 -141
  37. package/experimental/benchmarks/ListGrab.ff +23 -23
  38. package/experimental/benchmarks/ListGrab.java +55 -55
  39. package/experimental/benchmarks/Pyrotek45.ff +30 -30
  40. package/experimental/benchmarks/Pyrotek45.java +64 -64
  41. package/experimental/bidirectional/Bidi.ff +88 -88
  42. package/experimental/random/Index.ff +53 -53
  43. package/experimental/random/Process.ff +120 -120
  44. package/experimental/random/Scrape.ff +51 -51
  45. package/experimental/random/Symbols.ff +73 -73
  46. package/experimental/random/Tensor.ff +52 -52
  47. package/experimental/random/Units.ff +36 -36
  48. package/experimental/s3/S3TestAuthorizationHeader.ff +38 -38
  49. package/experimental/s3/S3TestPut.ff +15 -15
  50. package/experimental/tests/TestJson.ff +26 -26
  51. package/firefly.sh +0 -0
  52. package/fireflysite/Main.ff +13 -13
  53. package/lsp/.firefly/package.ff +1 -1
  54. package/lsp/CompletionHandler.ff +808 -808
  55. package/lsp/Handler.ff +714 -714
  56. package/lsp/HoverHandler.ff +79 -79
  57. package/lsp/LanguageServer.ff +272 -272
  58. package/lsp/SignatureHelpHandler.ff +55 -55
  59. package/lsp/SymbolHandler.ff +181 -181
  60. package/lsp/TestReferences.ff +16 -16
  61. package/lsp/TestReferencesCase.ff +7 -7
  62. package/lsp/stderr.txt +1 -1
  63. package/lsp/stdin.txt +10 -10
  64. package/lsp/stdout.txt +40 -40
  65. package/lux/.firefly/package.ff +1 -1
  66. package/lux/Css.ff +648 -648
  67. package/lux/CssTest.ff +48 -48
  68. package/lux/Lux.ff +487 -487
  69. package/lux/LuxEvent.ff +116 -116
  70. package/lux/Main.ff +128 -128
  71. package/lux/Main2.ff +144 -144
  72. package/output/js/ff/compiler/Builder.mjs +43 -43
  73. package/output/js/ff/compiler/Dependencies.mjs +3 -3
  74. package/output/js/ff/core/Array.mjs +59 -59
  75. package/output/js/ff/core/Atomic.mjs +36 -36
  76. package/output/js/ff/core/BrowserSystem.mjs +11 -11
  77. package/output/js/ff/core/BuildSystem.mjs +30 -30
  78. package/output/js/ff/core/Crypto.mjs +40 -40
  79. package/output/js/ff/core/HttpClient.mjs +24 -24
  80. package/output/js/ff/core/Json.mjs +147 -147
  81. package/output/js/ff/core/List.mjs +50 -50
  82. package/output/js/ff/core/Lock.mjs +97 -97
  83. package/output/js/ff/core/NodeSystem.mjs +83 -77
  84. package/output/js/ff/core/Ordering.mjs +8 -8
  85. package/output/js/ff/core/Path.mjs +231 -231
  86. package/output/js/ff/core/Random.mjs +56 -56
  87. package/output/js/ff/core/Task.mjs +31 -31
  88. package/package.json +1 -1
  89. package/rpc/.firefly/package.ff +1 -1
  90. package/rpc/Rpc.ff +69 -69
  91. package/s3/.firefly/package.ff +1 -1
  92. package/s3/S3.ff +90 -90
  93. package/unsafejs/UnsafeJs.ff +19 -19
  94. package/vscode/LICENSE.txt +21 -21
  95. package/vscode/Prepublish.ff +15 -15
  96. package/vscode/README.md +16 -16
  97. package/vscode/client/package.json +22 -22
  98. package/vscode/client/src/extension.ts +104 -104
  99. package/vscode/icons/firefly-icon.svg +10 -10
  100. package/vscode/language-configuration.json +61 -61
  101. package/vscode/package-lock.json +3623 -3623
  102. package/vscode/package.json +1 -1
  103. package/vscode/snippets.json +241 -241
  104. package/webserver/.firefly/include/package-lock.json +16 -16
  105. package/webserver/.firefly/include/package.json +5 -5
  106. package/webserver/.firefly/package.ff +2 -2
  107. package/webserver/WebServer.ff +685 -685
  108. package/websocket/.firefly/package.ff +1 -1
  109. package/websocket/WebSocket.ff +131 -131
package/core/List.ff CHANGED
@@ -1,486 +1,486 @@
1
- data List[T] {}
2
-
3
- new[T](): List[T]
4
- target js sync """
5
- return [];
6
- """
7
-
8
- fill[T](size: Int, value: T): List[T]
9
- target js sync """
10
- return new Array(size_).fill(value_);
11
- """
12
-
13
- fillBy[T](size: Int, body: Int => T): List[T]
14
- target js sync """
15
- return Array.from({length: size_}, (_, i) => body_(i));
16
- """
17
- target js async """
18
- const array = new Array(size_);
19
- for(let i = 0; i < size_; i++) {
20
- array[i] = await(body_(_i));
21
- }
22
- return array;
23
- """
24
-
25
- range(size: Int): List[Int]
26
- target js sync """
27
- return Array.from({length: size_}, (_, i) => i);
28
- """
29
-
30
- extend self[T]: List[T] {
31
-
32
- addAll(that: List[T]): List[T]
33
- target js sync "return self_.concat(that_)"
34
-
35
- isEmpty(): Bool
36
- target js sync "return self_.length === 0"
37
-
38
- size(): Int
39
- target js sync "return self_.length"
40
-
41
- get(index: Int): Option[T]
42
- target js sync """
43
- return index_ >= 0 && index_ < self_.length
44
- ? ff_core_Option.Some(self_[index_])
45
- : ff_core_Option.None()
46
- """
47
-
48
- grab(index: Int): T
49
- target js sync """
50
- return self_[index_] ?? internalGrab_(self_, index_);
51
- """
52
-
53
- first(): Option[T] {self.get(0)}
54
-
55
- last(): Option[T] {self.get(self.size() - 1)}
56
-
57
- grabFirst(): T {self.grab(0)}
58
-
59
- grabLast(): T {self.grab(self.size() - 1)}
60
-
61
- takeFirst(count: Int = 1): List[T]
62
- target js sync "return self_.slice(0, count_)"
63
-
64
- takeLast(count: Int = 1): List[T]
65
- target js sync "return self_.slice(-count_)"
66
-
67
- dropFirst(count: Int = 1): List[T]
68
- target js sync "return self_.slice(count_)"
69
-
70
- dropLast(count: Int = 1): List[T]
71
- target js sync "return self_.slice(0, self_.length - count_)"
72
-
73
- count(body: T => Bool): Int {
74
- mutable result = 0
75
- mutable i = 0
76
- while {i < self.size()} {
77
- if(body(self.grab(i))) {result += 1}
78
- i += 1
79
- }
80
- result
81
- }
82
-
83
- countWhile(body: T => Bool): Int {
84
- mutable i = 0
85
- while {i < self.size() && body(self.grab(i))} {
86
- i += 1
87
- }
88
- i
89
- }
90
-
91
- takeWhile(body: T => Bool): List[T] {
92
- let result = Array.new()
93
- mutable i = 0
94
- while {i < self.size() && body(self.grab(i))} {
95
- result.push(self.grab(i))
96
- i += 1
97
- }
98
- result.drain()
99
- }
100
-
101
- dropWhile(body: T => Bool): List[T] {
102
- let result = Array.new()
103
- mutable i = 0
104
- while {i < self.size() && body(self.grab(i))} {
105
- i += 1
106
- }
107
- while {i < self.size()} {
108
- result.push(self.grab(i))
109
- i += 1
110
- }
111
- result.drain()
112
- }
113
-
114
- partitionWhile(body: T => Bool): Pair[List[T], List[T]] {
115
- let first = Array.new()
116
- let second = Array.new()
117
- mutable i = 0
118
- while {i < self.size() && body(self.grab(i))} {
119
- first.push(self.grab(i))
120
- i += 1
121
- }
122
- while {i < self.size()} {
123
- second.push(self.grab(i))
124
- i += 1
125
- }
126
- Pair(first.drain(), second.drain())
127
- }
128
-
129
- pairs(): List[Pair[Int, T]] {
130
- mutable i = 0
131
- self.map {x =>
132
- let r = Pair(i, x)
133
- i += 1
134
- r
135
- }
136
- }
137
-
138
- slice(from: Int, until: Int): List[T] {
139
- self.dropFirst(from).takeFirst(until - from)
140
- }
141
-
142
- foldLeft[R](initial: R, body: (R, T) => R): R {
143
- mutable result = initial
144
- self.each {x =>
145
- result = body(result, x)
146
- }
147
- result
148
- }
149
-
150
- update(index: Int, value: T): List[T] {
151
- self.modify(index) {_ => value}
152
- }
153
-
154
- modify(index: Int, body: T => T): List[T]
155
- target js sync """
156
- if(index_ < 0 || index_ >= self_.length) {
157
- ff_core_Try.internalThrowGrabException_()
158
- }
159
- let result = self_.slice();
160
- result[index_] = body_(result[index_]);
161
- return result;
162
- """
163
- target js async """
164
- if(index_ < 0 || index_ >= self_.length) {
165
- ff_core_Try.internalThrowGrabException_()
166
- }
167
- let result = self_.slice();
168
- result[index_] = await body_(result[index_], $task);
169
- return result;
170
- """
171
-
172
- zip[S](that: List[S]): List[Pair[T, S]] {
173
- if(self.size() <= that.size()) {
174
- mutable i = -1
175
- self.map {x =>
176
- i += 1
177
- Pair(x, that.grab(i))
178
- }
179
- } else {
180
- mutable i = -1
181
- that.map {y =>
182
- i += 1
183
- Pair(self.grab(i), y)
184
- }
185
- }
186
- }
187
-
188
- chunk(chunkSize: Int): List[List[T]] {
189
- let results = Array.new()
190
- let result = Array.new()
191
- mutable added = 0
192
- self.each {item =>
193
- if(added < chunkSize) {
194
- added += 1
195
- } else {
196
- results.push(result.drain())
197
- added = 1
198
- }
199
- result.push(item)
200
- }
201
- if(added != 0) {
202
- results.push(result.drain())
203
- }
204
- results.drain()
205
- }
206
-
207
- toStream(cycle: Bool = False): Stream[T] {
208
- mutable index = 0
209
- Stream.new {
210
- if(index < self.size()) {
211
- let result = self.grab(index)
212
- index += 1
213
- result
214
- } elseIf {cycle && index != 0} {
215
- let result = self.grab(0)
216
- index = 1
217
- result
218
- }
219
- }
220
- }
221
-
222
- toArray(): Array[T]
223
- target js sync "return {array: self_.slice()}"
224
-
225
- each(body: T => Unit): Unit
226
- target js sync """
227
- return self_.forEach(body_);
228
- """
229
- target js async """
230
- for(let i = 0; i < self_.length; i++) {
231
- await body_(self_[i], $task)
232
- }
233
- """
234
-
235
- eachWhile(body: T => Bool): Unit
236
- target js sync "for(const value of self_) if(!body_(value)) break"
237
- target js async "for(const value of self_) if(!await body_(value, $task)) break"
238
-
239
- all(body: T => Bool): Bool {
240
- mutable result = True
241
- self.eachWhile {x =>
242
- result = result && body(x)
243
- result
244
- }
245
- result
246
- }
247
-
248
- any(body: T => Bool): Bool {
249
- mutable result = False
250
- self.eachWhile {x =>
251
- result = result || body(x)
252
- (!result)
253
- }
254
- result
255
- }
256
-
257
- indexWhere(body: T => Bool): Option[Int] {
258
- mutable result = None
259
- mutable i = 0
260
- self.eachWhile {x =>
261
- if(body(x)) {
262
- result = Some(i)
263
- False
264
- } else {
265
- i += 1
266
- True
267
- }
268
- }
269
- result
270
- }
271
-
272
- find(body: T => Bool): Option[T] {
273
- mutable result = None
274
- self.eachWhile {x =>
275
- if(body(x)) {
276
- result = Some(x)
277
- False
278
- } else {
279
- True
280
- }
281
- }
282
- result
283
- }
284
-
285
- filter(body: T => Bool): List[T] {
286
- let result = Array.new()
287
- self.each {x =>
288
- if(body(x)) {
289
- result.push(x)
290
- }
291
- }
292
- result.drain()
293
- }
294
-
295
- partition(body: T => Bool): Pair[List[T], List[T]] {
296
- Pair(self.filter(body), self.filter {!body(_)})
297
- }
298
-
299
- map[S](body: T => S): List[S]
300
- target js sync """
301
- return self_.map(body_)
302
- """
303
- target js async """
304
- let result = [];
305
- for(let i = 0; i < self_.length; i++) {
306
- result.push(await body_(self_[i], $task));
307
- }
308
- return result;
309
- """
310
-
311
- flatMap[S](body: T => List[S]): List[S] {
312
- let results = Array.new()
313
- self.each {x =>
314
- results.pushList(body(x))
315
- }
316
- results.drain()
317
- }
318
-
319
- collect[R](body: T => Option[R]): List[R] {
320
- mutable result = Array.new()
321
- self.each {x =>
322
- body(x).each {result.push(_)}
323
- }
324
- result.drain()
325
- }
326
-
327
- collectFirst[R](body: T => Option[R]): Option[R] {
328
- mutable result = None
329
- self.eachWhile {x =>
330
- body(x).{
331
- | None =>
332
- True
333
- | o =>
334
- result = o
335
- False
336
- }
337
- }
338
- result
339
- }
340
-
341
- sortBy[S: Order](body: T => S): List[T] {
342
- self.sortWith {Ordering.compare(body(_), body(_))}
343
- }
344
-
345
- sortWith(ordering: (T, T) => Ordering): List[T] {
346
- let array = self.toArray()
347
- array.sortWith(ordering)
348
- array.drain()
349
- }
350
-
351
- reverse(): List[T] {
352
- 1.to(self.size()).map {i =>
353
- self.grab(self.size() - i)
354
- }
355
- }
356
-
357
- separate(separator: List[T]): List[T] {
358
- let array = Array.new()
359
- self.pairs().each {| Pair(i, x) =>
360
- if(i != 0) {
361
- array.pushList(separator)
362
- }
363
- array.push(x)
364
- }
365
- array.drain()
366
- }
367
-
368
- }
369
-
370
- extend self[T: Order]: List[T] {
371
-
372
- sort(): List[T] {
373
- self.sortWith(Ordering.compare)
374
- }
375
-
376
- toSet(): Set[T] {
377
- self.foldLeft(Set.new()) {_.add(_)}
378
- }
379
-
380
- distinct(): List[T] {
381
- mutable seen = [].toSet()
382
- self.filter {
383
- | item {!seen.contains(item)} =>
384
- seen = seen.add(item)
385
- True
386
- | _ =>
387
- False
388
- }
389
- }
390
-
391
- }
392
-
393
- extend self[T]: List[List[T]] {
394
-
395
- flatten(): List[T] {
396
- let result = Array.new()
397
- self.each {xs =>
398
- result.pushList(xs)
399
- }
400
- result.drain()
401
- }
402
-
403
- }
404
-
405
- extend self[K: Order, V]: List[Pair[K, V]] {
406
-
407
- toMap(): Map[K, V] {
408
- self.foldLeft(Map.new()) {| map, Pair(key, value) => map.add(key, value)}
409
- }
410
-
411
- group(): Map[K, List[V]] {
412
- mutable map = Map.new[K, Array[V]]()
413
- self.each {| Pair(k, v) => map = map.push(k, v)}
414
- map.mapValues {_, v => v.drain()}
415
- }
416
-
417
- }
418
-
419
- extend self[K, V]: List[Pair[K, V]] {
420
-
421
- unzip(): Pair[List[K], List[V]] {
422
- let first = Array.new()
423
- let second = Array.new()
424
- self.each {| Pair(x, y) =>
425
- first.push(x)
426
- second.push(y)
427
- }
428
- Pair(first.drain(), second.drain())
429
- }
430
-
431
- }
432
-
433
- extend self: List[String] {
434
-
435
- join(separator: String = ""): String
436
- target js sync "return self_.join(separator_)"
437
-
438
- }
439
-
440
- instance List[T: Show]: Show {
441
- show(value: List[T]): String {
442
- let array = Array.new()
443
- array.push("[")
444
- value.each {x =>
445
- if(array.size() > 1) {array.push(", ")}
446
- array.push(Show.show(x))
447
- }
448
- array.push("]")
449
- array.join()
450
- }
451
- }
452
-
453
- instance List[T: Equal]: Equal {
454
- equals(x: List[T], y: List[T]): Bool {
455
- if(internalSame(x, y)) {True} else:
456
- if(x.size() != y.size()) {False} else:
457
- mutable i = -1
458
- x.all {l =>
459
- i += 1
460
- l == y.grab(i)
461
- }
462
- }
463
- }
464
-
465
- instance List[T: Order]: Order {
466
- compare(x: List[T], y: List[T]): Ordering {
467
- if(internalSame(x, y)) {OrderingSame} else:
468
- let size = x.size().min(y.size())
469
- mutable i = 0
470
- mutable ordering = OrderingSame
471
- while {ordering == OrderingSame && i < size} {
472
- ordering = Ordering.compare(x.grab(i), y.grab(i))
473
- i += 1
474
- }
475
- if(ordering != OrderingSame) {ordering} else:
476
- Ordering.compare(x.size(), y.size())
477
- }
478
- }
479
-
480
- internalSame[T](left: List[T], right: List[T]): Bool
481
- target js sync "return left_ === right_"
482
-
483
- internalGrab[T](self: List[T], index: Int): T
484
- target js sync """
485
- return index_ < 0 || index_ >= self_.length ? ff_core_Try.internalThrowGrabException_() : self_[index_];
486
- """
1
+ data List[T] {}
2
+
3
+ new[T](): List[T]
4
+ target js sync """
5
+ return [];
6
+ """
7
+
8
+ fill[T](size: Int, value: T): List[T]
9
+ target js sync """
10
+ return new Array(size_).fill(value_);
11
+ """
12
+
13
+ fillBy[T](size: Int, body: Int => T): List[T]
14
+ target js sync """
15
+ return Array.from({length: size_}, (_, i) => body_(i));
16
+ """
17
+ target js async """
18
+ const array = new Array(size_);
19
+ for(let i = 0; i < size_; i++) {
20
+ array[i] = await(body_(_i));
21
+ }
22
+ return array;
23
+ """
24
+
25
+ range(size: Int): List[Int]
26
+ target js sync """
27
+ return Array.from({length: size_}, (_, i) => i);
28
+ """
29
+
30
+ extend self[T]: List[T] {
31
+
32
+ addAll(that: List[T]): List[T]
33
+ target js sync "return self_.concat(that_)"
34
+
35
+ isEmpty(): Bool
36
+ target js sync "return self_.length === 0"
37
+
38
+ size(): Int
39
+ target js sync "return self_.length"
40
+
41
+ get(index: Int): Option[T]
42
+ target js sync """
43
+ return index_ >= 0 && index_ < self_.length
44
+ ? ff_core_Option.Some(self_[index_])
45
+ : ff_core_Option.None()
46
+ """
47
+
48
+ grab(index: Int): T
49
+ target js sync """
50
+ return self_[index_] ?? internalGrab_(self_, index_);
51
+ """
52
+
53
+ first(): Option[T] {self.get(0)}
54
+
55
+ last(): Option[T] {self.get(self.size() - 1)}
56
+
57
+ grabFirst(): T {self.grab(0)}
58
+
59
+ grabLast(): T {self.grab(self.size() - 1)}
60
+
61
+ takeFirst(count: Int = 1): List[T]
62
+ target js sync "return self_.slice(0, count_)"
63
+
64
+ takeLast(count: Int = 1): List[T]
65
+ target js sync "return self_.slice(-count_)"
66
+
67
+ dropFirst(count: Int = 1): List[T]
68
+ target js sync "return self_.slice(count_)"
69
+
70
+ dropLast(count: Int = 1): List[T]
71
+ target js sync "return self_.slice(0, self_.length - count_)"
72
+
73
+ count(body: T => Bool): Int {
74
+ mutable result = 0
75
+ mutable i = 0
76
+ while {i < self.size()} {
77
+ if(body(self.grab(i))) {result += 1}
78
+ i += 1
79
+ }
80
+ result
81
+ }
82
+
83
+ countWhile(body: T => Bool): Int {
84
+ mutable i = 0
85
+ while {i < self.size() && body(self.grab(i))} {
86
+ i += 1
87
+ }
88
+ i
89
+ }
90
+
91
+ takeWhile(body: T => Bool): List[T] {
92
+ let result = Array.new()
93
+ mutable i = 0
94
+ while {i < self.size() && body(self.grab(i))} {
95
+ result.push(self.grab(i))
96
+ i += 1
97
+ }
98
+ result.drain()
99
+ }
100
+
101
+ dropWhile(body: T => Bool): List[T] {
102
+ let result = Array.new()
103
+ mutable i = 0
104
+ while {i < self.size() && body(self.grab(i))} {
105
+ i += 1
106
+ }
107
+ while {i < self.size()} {
108
+ result.push(self.grab(i))
109
+ i += 1
110
+ }
111
+ result.drain()
112
+ }
113
+
114
+ partitionWhile(body: T => Bool): Pair[List[T], List[T]] {
115
+ let first = Array.new()
116
+ let second = Array.new()
117
+ mutable i = 0
118
+ while {i < self.size() && body(self.grab(i))} {
119
+ first.push(self.grab(i))
120
+ i += 1
121
+ }
122
+ while {i < self.size()} {
123
+ second.push(self.grab(i))
124
+ i += 1
125
+ }
126
+ Pair(first.drain(), second.drain())
127
+ }
128
+
129
+ pairs(): List[Pair[Int, T]] {
130
+ mutable i = 0
131
+ self.map {x =>
132
+ let r = Pair(i, x)
133
+ i += 1
134
+ r
135
+ }
136
+ }
137
+
138
+ slice(from: Int, until: Int): List[T] {
139
+ self.dropFirst(from).takeFirst(until - from)
140
+ }
141
+
142
+ foldLeft[R](initial: R, body: (R, T) => R): R {
143
+ mutable result = initial
144
+ self.each {x =>
145
+ result = body(result, x)
146
+ }
147
+ result
148
+ }
149
+
150
+ update(index: Int, value: T): List[T] {
151
+ self.modify(index) {_ => value}
152
+ }
153
+
154
+ modify(index: Int, body: T => T): List[T]
155
+ target js sync """
156
+ if(index_ < 0 || index_ >= self_.length) {
157
+ ff_core_Try.internalThrowGrabException_()
158
+ }
159
+ let result = self_.slice();
160
+ result[index_] = body_(result[index_]);
161
+ return result;
162
+ """
163
+ target js async """
164
+ if(index_ < 0 || index_ >= self_.length) {
165
+ ff_core_Try.internalThrowGrabException_()
166
+ }
167
+ let result = self_.slice();
168
+ result[index_] = await body_(result[index_], $task);
169
+ return result;
170
+ """
171
+
172
+ zip[S](that: List[S]): List[Pair[T, S]] {
173
+ if(self.size() <= that.size()) {
174
+ mutable i = -1
175
+ self.map {x =>
176
+ i += 1
177
+ Pair(x, that.grab(i))
178
+ }
179
+ } else {
180
+ mutable i = -1
181
+ that.map {y =>
182
+ i += 1
183
+ Pair(self.grab(i), y)
184
+ }
185
+ }
186
+ }
187
+
188
+ chunk(chunkSize: Int): List[List[T]] {
189
+ let results = Array.new()
190
+ let result = Array.new()
191
+ mutable added = 0
192
+ self.each {item =>
193
+ if(added < chunkSize) {
194
+ added += 1
195
+ } else {
196
+ results.push(result.drain())
197
+ added = 1
198
+ }
199
+ result.push(item)
200
+ }
201
+ if(added != 0) {
202
+ results.push(result.drain())
203
+ }
204
+ results.drain()
205
+ }
206
+
207
+ toStream(cycle: Bool = False): Stream[T] {
208
+ mutable index = 0
209
+ Stream.new {
210
+ if(index < self.size()) {
211
+ let result = self.grab(index)
212
+ index += 1
213
+ result
214
+ } elseIf {cycle && index != 0} {
215
+ let result = self.grab(0)
216
+ index = 1
217
+ result
218
+ }
219
+ }
220
+ }
221
+
222
+ toArray(): Array[T]
223
+ target js sync "return {array: self_.slice()}"
224
+
225
+ each(body: T => Unit): Unit
226
+ target js sync """
227
+ return self_.forEach(body_);
228
+ """
229
+ target js async """
230
+ for(let i = 0; i < self_.length; i++) {
231
+ await body_(self_[i], $task)
232
+ }
233
+ """
234
+
235
+ eachWhile(body: T => Bool): Unit
236
+ target js sync "for(const value of self_) if(!body_(value)) break"
237
+ target js async "for(const value of self_) if(!await body_(value, $task)) break"
238
+
239
+ all(body: T => Bool): Bool {
240
+ mutable result = True
241
+ self.eachWhile {x =>
242
+ result = result && body(x)
243
+ result
244
+ }
245
+ result
246
+ }
247
+
248
+ any(body: T => Bool): Bool {
249
+ mutable result = False
250
+ self.eachWhile {x =>
251
+ result = result || body(x)
252
+ (!result)
253
+ }
254
+ result
255
+ }
256
+
257
+ indexWhere(body: T => Bool): Option[Int] {
258
+ mutable result = None
259
+ mutable i = 0
260
+ self.eachWhile {x =>
261
+ if(body(x)) {
262
+ result = Some(i)
263
+ False
264
+ } else {
265
+ i += 1
266
+ True
267
+ }
268
+ }
269
+ result
270
+ }
271
+
272
+ find(body: T => Bool): Option[T] {
273
+ mutable result = None
274
+ self.eachWhile {x =>
275
+ if(body(x)) {
276
+ result = Some(x)
277
+ False
278
+ } else {
279
+ True
280
+ }
281
+ }
282
+ result
283
+ }
284
+
285
+ filter(body: T => Bool): List[T] {
286
+ let result = Array.new()
287
+ self.each {x =>
288
+ if(body(x)) {
289
+ result.push(x)
290
+ }
291
+ }
292
+ result.drain()
293
+ }
294
+
295
+ partition(body: T => Bool): Pair[List[T], List[T]] {
296
+ Pair(self.filter(body), self.filter {!body(_)})
297
+ }
298
+
299
+ map[S](body: T => S): List[S]
300
+ target js sync """
301
+ return self_.map(body_)
302
+ """
303
+ target js async """
304
+ let result = [];
305
+ for(let i = 0; i < self_.length; i++) {
306
+ result.push(await body_(self_[i], $task));
307
+ }
308
+ return result;
309
+ """
310
+
311
+ flatMap[S](body: T => List[S]): List[S] {
312
+ let results = Array.new()
313
+ self.each {x =>
314
+ results.pushList(body(x))
315
+ }
316
+ results.drain()
317
+ }
318
+
319
+ collect[R](body: T => Option[R]): List[R] {
320
+ mutable result = Array.new()
321
+ self.each {x =>
322
+ body(x).each {result.push(_)}
323
+ }
324
+ result.drain()
325
+ }
326
+
327
+ collectFirst[R](body: T => Option[R]): Option[R] {
328
+ mutable result = None
329
+ self.eachWhile {x =>
330
+ body(x).{
331
+ | None =>
332
+ True
333
+ | o =>
334
+ result = o
335
+ False
336
+ }
337
+ }
338
+ result
339
+ }
340
+
341
+ sortBy[S: Order](body: T => S): List[T] {
342
+ self.sortWith {Ordering.compare(body(_), body(_))}
343
+ }
344
+
345
+ sortWith(ordering: (T, T) => Ordering): List[T] {
346
+ let array = self.toArray()
347
+ array.sortWith(ordering)
348
+ array.drain()
349
+ }
350
+
351
+ reverse(): List[T] {
352
+ 1.to(self.size()).map {i =>
353
+ self.grab(self.size() - i)
354
+ }
355
+ }
356
+
357
+ separate(separator: List[T]): List[T] {
358
+ let array = Array.new()
359
+ self.pairs().each {| Pair(i, x) =>
360
+ if(i != 0) {
361
+ array.pushList(separator)
362
+ }
363
+ array.push(x)
364
+ }
365
+ array.drain()
366
+ }
367
+
368
+ }
369
+
370
+ extend self[T: Order]: List[T] {
371
+
372
+ sort(): List[T] {
373
+ self.sortWith(Ordering.compare)
374
+ }
375
+
376
+ toSet(): Set[T] {
377
+ self.foldLeft(Set.new()) {_.add(_)}
378
+ }
379
+
380
+ distinct(): List[T] {
381
+ mutable seen = [].toSet()
382
+ self.filter {
383
+ | item {!seen.contains(item)} =>
384
+ seen = seen.add(item)
385
+ True
386
+ | _ =>
387
+ False
388
+ }
389
+ }
390
+
391
+ }
392
+
393
+ extend self[T]: List[List[T]] {
394
+
395
+ flatten(): List[T] {
396
+ let result = Array.new()
397
+ self.each {xs =>
398
+ result.pushList(xs)
399
+ }
400
+ result.drain()
401
+ }
402
+
403
+ }
404
+
405
+ extend self[K: Order, V]: List[Pair[K, V]] {
406
+
407
+ toMap(): Map[K, V] {
408
+ self.foldLeft(Map.new()) {| map, Pair(key, value) => map.add(key, value)}
409
+ }
410
+
411
+ group(): Map[K, List[V]] {
412
+ mutable map = Map.new[K, Array[V]]()
413
+ self.each {| Pair(k, v) => map = map.push(k, v)}
414
+ map.mapValues {_, v => v.drain()}
415
+ }
416
+
417
+ }
418
+
419
+ extend self[K, V]: List[Pair[K, V]] {
420
+
421
+ unzip(): Pair[List[K], List[V]] {
422
+ let first = Array.new()
423
+ let second = Array.new()
424
+ self.each {| Pair(x, y) =>
425
+ first.push(x)
426
+ second.push(y)
427
+ }
428
+ Pair(first.drain(), second.drain())
429
+ }
430
+
431
+ }
432
+
433
+ extend self: List[String] {
434
+
435
+ join(separator: String = ""): String
436
+ target js sync "return self_.join(separator_)"
437
+
438
+ }
439
+
440
+ instance List[T: Show]: Show {
441
+ show(value: List[T]): String {
442
+ let array = Array.new()
443
+ array.push("[")
444
+ value.each {x =>
445
+ if(array.size() > 1) {array.push(", ")}
446
+ array.push(Show.show(x))
447
+ }
448
+ array.push("]")
449
+ array.join()
450
+ }
451
+ }
452
+
453
+ instance List[T: Equal]: Equal {
454
+ equals(x: List[T], y: List[T]): Bool {
455
+ if(internalSame(x, y)) {True} else:
456
+ if(x.size() != y.size()) {False} else:
457
+ mutable i = -1
458
+ x.all {l =>
459
+ i += 1
460
+ l == y.grab(i)
461
+ }
462
+ }
463
+ }
464
+
465
+ instance List[T: Order]: Order {
466
+ compare(x: List[T], y: List[T]): Ordering {
467
+ if(internalSame(x, y)) {OrderingSame} else:
468
+ let size = x.size().min(y.size())
469
+ mutable i = 0
470
+ mutable ordering = OrderingSame
471
+ while {ordering == OrderingSame && i < size} {
472
+ ordering = Ordering.compare(x.grab(i), y.grab(i))
473
+ i += 1
474
+ }
475
+ if(ordering != OrderingSame) {ordering} else:
476
+ Ordering.compare(x.size(), y.size())
477
+ }
478
+ }
479
+
480
+ internalSame[T](left: List[T], right: List[T]): Bool
481
+ target js sync "return left_ === right_"
482
+
483
+ internalGrab[T](self: List[T], index: Int): T
484
+ target js sync """
485
+ return index_ < 0 || index_ >= self_.length ? ff_core_Try.internalThrowGrabException_() : self_[index_];
486
+ """