@grain/stdlib 0.5.2 → 0.5.4
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/CHANGELOG.md +59 -0
- package/array.gr +61 -1
- package/array.md +113 -0
- package/bigint.md +30 -30
- package/buffer.gr +24 -22
- package/char.gr +2 -2
- package/float32.md +3 -3
- package/float64.md +3 -3
- package/immutablemap.gr +493 -0
- package/immutablemap.md +479 -0
- package/immutablepriorityqueue.gr +360 -0
- package/immutablepriorityqueue.md +291 -0
- package/immutableset.gr +498 -0
- package/immutableset.md +449 -0
- package/list.gr +75 -2
- package/list.md +110 -0
- package/map.gr +1 -2
- package/marshal.gr +1058 -0
- package/marshal.md +76 -0
- package/number.gr +689 -23
- package/number.md +362 -27
- package/package.json +1 -1
- package/pervasives.gr +16 -5
- package/pervasives.md +28 -0
- package/priorityqueue.gr +261 -0
- package/priorityqueue.md +309 -0
- package/queue.gr +14 -1
- package/queue.md +16 -1
- package/regex.gr +90 -67
- package/runtime/bigint.gr +4 -4
- package/runtime/compare.gr +179 -0
- package/runtime/compare.md +6 -0
- package/runtime/equal.gr +3 -3
- package/runtime/exception.gr +9 -5
- package/runtime/exception.md +8 -2
- package/runtime/gc.gr +2 -1
- package/runtime/malloc.gr +1 -3
- package/runtime/numberUtils.gr +11 -11
- package/runtime/numbers.gr +423 -100
- package/runtime/numbers.md +50 -0
- package/runtime/string.gr +4 -2
- package/set.gr +26 -27
- package/stack.gr +12 -0
- package/stack.md +15 -0
- package/string.gr +409 -53
- package/string.md +164 -1
- package/sys/file.gr +4 -4
- package/sys/file.md +3 -3
- package/sys/process.gr +3 -3
- package/sys/process.md +3 -3
- package/sys/random.gr +2 -2
- package/sys/random.md +2 -2
- package/sys/time.gr +2 -2
- package/sys/time.md +2 -2
package/immutableset.md
ADDED
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: ImmutableSet
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
An ImmutableSet is a collection of unique values. Operations on an ImmutableSet do not mutate the set's internal state.
|
|
6
|
+
|
|
7
|
+
<details disabled>
|
|
8
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
9
|
+
No other changes yet.
|
|
10
|
+
</details>
|
|
11
|
+
|
|
12
|
+
```grain
|
|
13
|
+
import ImmutableSet from "immutableset"
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Types
|
|
17
|
+
|
|
18
|
+
Type declarations included in the ImmutableSet module.
|
|
19
|
+
|
|
20
|
+
### ImmutableSet.**ImmutableSet**
|
|
21
|
+
|
|
22
|
+
```grain
|
|
23
|
+
type ImmutableSet<a>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Values
|
|
27
|
+
|
|
28
|
+
Functions and constants for working with ImmutableSets.
|
|
29
|
+
|
|
30
|
+
### ImmutableSet.**empty**
|
|
31
|
+
|
|
32
|
+
<details disabled>
|
|
33
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
34
|
+
No other changes yet.
|
|
35
|
+
</details>
|
|
36
|
+
|
|
37
|
+
```grain
|
|
38
|
+
empty : ImmutableSet<a>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
An empty set
|
|
42
|
+
|
|
43
|
+
### ImmutableSet.**size**
|
|
44
|
+
|
|
45
|
+
<details disabled>
|
|
46
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
47
|
+
No other changes yet.
|
|
48
|
+
</details>
|
|
49
|
+
|
|
50
|
+
```grain
|
|
51
|
+
size : ImmutableSet<a> -> Number
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Provides the count of values within the set.
|
|
55
|
+
|
|
56
|
+
Parameters:
|
|
57
|
+
|
|
58
|
+
|param|type|description|
|
|
59
|
+
|-----|----|-----------|
|
|
60
|
+
|`set`|`ImmutableSet<a>`|The set to inspect|
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
|
|
64
|
+
|type|description|
|
|
65
|
+
|----|-----------|
|
|
66
|
+
|`Number`|The count of elements in the set|
|
|
67
|
+
|
|
68
|
+
### ImmutableSet.**isEmpty**
|
|
69
|
+
|
|
70
|
+
<details disabled>
|
|
71
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
72
|
+
No other changes yet.
|
|
73
|
+
</details>
|
|
74
|
+
|
|
75
|
+
```grain
|
|
76
|
+
isEmpty : ImmutableSet<a> -> Bool
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Determines if the set contains no elements.
|
|
80
|
+
|
|
81
|
+
Parameters:
|
|
82
|
+
|
|
83
|
+
|param|type|description|
|
|
84
|
+
|-----|----|-----------|
|
|
85
|
+
|`set`|`ImmutableSet<a>`|The set to inspect|
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
|
|
89
|
+
|type|description|
|
|
90
|
+
|----|-----------|
|
|
91
|
+
|`Bool`|`true` if the given set is empty or `false` otherwise|
|
|
92
|
+
|
|
93
|
+
### ImmutableSet.**add**
|
|
94
|
+
|
|
95
|
+
<details disabled>
|
|
96
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
97
|
+
No other changes yet.
|
|
98
|
+
</details>
|
|
99
|
+
|
|
100
|
+
```grain
|
|
101
|
+
add : (a, ImmutableSet<a>) -> ImmutableSet<a>
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Produces a new set by inserting the given value into the set. If the value
|
|
105
|
+
already exists, the new set will have the same elements as the input set.
|
|
106
|
+
|
|
107
|
+
Parameters:
|
|
108
|
+
|
|
109
|
+
|param|type|description|
|
|
110
|
+
|-----|----|-----------|
|
|
111
|
+
|`key`|`a`|The value to add|
|
|
112
|
+
|`set`|`ImmutableSet<a>`|The base set|
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
|
|
116
|
+
|type|description|
|
|
117
|
+
|----|-----------|
|
|
118
|
+
|`ImmutableSet<a>`|A new set containing the new element|
|
|
119
|
+
|
|
120
|
+
### ImmutableSet.**contains**
|
|
121
|
+
|
|
122
|
+
<details disabled>
|
|
123
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
124
|
+
No other changes yet.
|
|
125
|
+
</details>
|
|
126
|
+
|
|
127
|
+
```grain
|
|
128
|
+
contains : (a, ImmutableSet<a>) -> Bool
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Determines if the set contains the given value.
|
|
132
|
+
|
|
133
|
+
Parameters:
|
|
134
|
+
|
|
135
|
+
|param|type|description|
|
|
136
|
+
|-----|----|-----------|
|
|
137
|
+
|`key`|`a`|The value to search for|
|
|
138
|
+
|`set`|`ImmutableSet<a>`|The set to search|
|
|
139
|
+
|
|
140
|
+
Returns:
|
|
141
|
+
|
|
142
|
+
|type|description|
|
|
143
|
+
|----|-----------|
|
|
144
|
+
|`Bool`|`true` if the set contains the given value or `false` otherwise|
|
|
145
|
+
|
|
146
|
+
### ImmutableSet.**remove**
|
|
147
|
+
|
|
148
|
+
<details disabled>
|
|
149
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
150
|
+
No other changes yet.
|
|
151
|
+
</details>
|
|
152
|
+
|
|
153
|
+
```grain
|
|
154
|
+
remove : (a, ImmutableSet<a>) -> ImmutableSet<a>
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Produces a new set without the given element. If the value doesn't exist in
|
|
158
|
+
the set, the set will be returned unmodified.
|
|
159
|
+
|
|
160
|
+
Parameters:
|
|
161
|
+
|
|
162
|
+
|param|type|description|
|
|
163
|
+
|-----|----|-----------|
|
|
164
|
+
|`key`|`a`|The value to exclude|
|
|
165
|
+
|`set`|`ImmutableSet<a>`|The set to exclude from|
|
|
166
|
+
|
|
167
|
+
Returns:
|
|
168
|
+
|
|
169
|
+
|type|description|
|
|
170
|
+
|----|-----------|
|
|
171
|
+
|`ImmutableSet<a>`|A new set without the excluded element|
|
|
172
|
+
|
|
173
|
+
### ImmutableSet.**forEach**
|
|
174
|
+
|
|
175
|
+
<details disabled>
|
|
176
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
177
|
+
No other changes yet.
|
|
178
|
+
</details>
|
|
179
|
+
|
|
180
|
+
```grain
|
|
181
|
+
forEach : ((a -> Void), ImmutableSet<a>) -> Void
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Iterates the set, calling an iterator function on each element.
|
|
185
|
+
|
|
186
|
+
Parameters:
|
|
187
|
+
|
|
188
|
+
|param|type|description|
|
|
189
|
+
|-----|----|-----------|
|
|
190
|
+
|`fn`|`a -> Void`|The iterator function to call with each element|
|
|
191
|
+
|`set`|`ImmutableSet<a>`|The set to iterate|
|
|
192
|
+
|
|
193
|
+
### ImmutableSet.**reduce**
|
|
194
|
+
|
|
195
|
+
<details disabled>
|
|
196
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
197
|
+
No other changes yet.
|
|
198
|
+
</details>
|
|
199
|
+
|
|
200
|
+
```grain
|
|
201
|
+
reduce : (((a, b) -> a), a, ImmutableSet<b>) -> a
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Combines all elements of a set using a reducer function.
|
|
205
|
+
|
|
206
|
+
Parameters:
|
|
207
|
+
|
|
208
|
+
|param|type|description|
|
|
209
|
+
|-----|----|-----------|
|
|
210
|
+
|`fn`|`(a, b) -> a`|The reducer function to call on each element, where the value returned will be the next accumulator value|
|
|
211
|
+
|`init`|`a`|The initial value to use for the accumulator on the first iteration|
|
|
212
|
+
|`set`|`ImmutableSet<b>`|The set to iterate|
|
|
213
|
+
|
|
214
|
+
Returns:
|
|
215
|
+
|
|
216
|
+
|type|description|
|
|
217
|
+
|----|-----------|
|
|
218
|
+
|`a`|The final accumulator returned from `fn`|
|
|
219
|
+
|
|
220
|
+
### ImmutableSet.**filter**
|
|
221
|
+
|
|
222
|
+
<details disabled>
|
|
223
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
224
|
+
No other changes yet.
|
|
225
|
+
</details>
|
|
226
|
+
|
|
227
|
+
```grain
|
|
228
|
+
filter : ((a -> Bool), ImmutableSet<a>) -> ImmutableSet<a>
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Produces a new set without the elements from the input set where a predicate function returns `false`.
|
|
232
|
+
|
|
233
|
+
Parameters:
|
|
234
|
+
|
|
235
|
+
|param|type|description|
|
|
236
|
+
|-----|----|-----------|
|
|
237
|
+
|`fn`|`a -> Bool`|The predicate function to indicate which elements to exclude from the set, where returning `false` indicates the value should be excluded|
|
|
238
|
+
|`set`|`ImmutableSet<a>`|The set to iterate|
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
|
|
242
|
+
|type|description|
|
|
243
|
+
|----|-----------|
|
|
244
|
+
|`ImmutableSet<a>`|A new set excluding the elements not fulfilling the predicate|
|
|
245
|
+
|
|
246
|
+
### ImmutableSet.**reject**
|
|
247
|
+
|
|
248
|
+
<details disabled>
|
|
249
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
250
|
+
No other changes yet.
|
|
251
|
+
</details>
|
|
252
|
+
|
|
253
|
+
```grain
|
|
254
|
+
reject : ((a -> Bool), ImmutableSet<a>) -> ImmutableSet<a>
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
Produces a new set without the elements from the input set where a predicate function returns `true`.
|
|
258
|
+
|
|
259
|
+
Parameters:
|
|
260
|
+
|
|
261
|
+
|param|type|description|
|
|
262
|
+
|-----|----|-----------|
|
|
263
|
+
|`fn`|`a -> Bool`|The predicate function to indicate which elements to exclude from the set, where returning `true` indicates the value should be excluded|
|
|
264
|
+
|`set`|`ImmutableSet<a>`|The set to iterate|
|
|
265
|
+
|
|
266
|
+
Returns:
|
|
267
|
+
|
|
268
|
+
|type|description|
|
|
269
|
+
|----|-----------|
|
|
270
|
+
|`ImmutableSet<a>`|A new set excluding the elements fulfilling the predicate|
|
|
271
|
+
|
|
272
|
+
### ImmutableSet.**union**
|
|
273
|
+
|
|
274
|
+
<details disabled>
|
|
275
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
276
|
+
No other changes yet.
|
|
277
|
+
</details>
|
|
278
|
+
|
|
279
|
+
```grain
|
|
280
|
+
union : (ImmutableSet<a>, ImmutableSet<a>) -> ImmutableSet<a>
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
Combines two sets into a single set containing all elements from both sets.
|
|
284
|
+
|
|
285
|
+
Parameters:
|
|
286
|
+
|
|
287
|
+
|param|type|description|
|
|
288
|
+
|-----|----|-----------|
|
|
289
|
+
|`set1`|`ImmutableSet<a>`|The first set to combine|
|
|
290
|
+
|`set2`|`ImmutableSet<a>`|The second set to combine|
|
|
291
|
+
|
|
292
|
+
Returns:
|
|
293
|
+
|
|
294
|
+
|type|description|
|
|
295
|
+
|----|-----------|
|
|
296
|
+
|`ImmutableSet<a>`|A set containing all elements of both sets|
|
|
297
|
+
|
|
298
|
+
### ImmutableSet.**diff**
|
|
299
|
+
|
|
300
|
+
<details disabled>
|
|
301
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
302
|
+
No other changes yet.
|
|
303
|
+
</details>
|
|
304
|
+
|
|
305
|
+
```grain
|
|
306
|
+
diff : (ImmutableSet<a>, ImmutableSet<a>) -> ImmutableSet<a>
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
Combines two sets into a single set containing only the elements not shared between both sets.
|
|
310
|
+
|
|
311
|
+
Parameters:
|
|
312
|
+
|
|
313
|
+
|param|type|description|
|
|
314
|
+
|-----|----|-----------|
|
|
315
|
+
|`set1`|`ImmutableSet<a>`|The first set to combine|
|
|
316
|
+
|`set2`|`ImmutableSet<a>`|The second set to combine|
|
|
317
|
+
|
|
318
|
+
Returns:
|
|
319
|
+
|
|
320
|
+
|type|description|
|
|
321
|
+
|----|-----------|
|
|
322
|
+
|`ImmutableSet<a>`|A set containing only unshared elements from both sets|
|
|
323
|
+
|
|
324
|
+
### ImmutableSet.**intersect**
|
|
325
|
+
|
|
326
|
+
<details disabled>
|
|
327
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
328
|
+
No other changes yet.
|
|
329
|
+
</details>
|
|
330
|
+
|
|
331
|
+
```grain
|
|
332
|
+
intersect : (ImmutableSet<a>, ImmutableSet<a>) -> ImmutableSet<a>
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Combines two sets into a single set containing only the elements shared between both sets.
|
|
336
|
+
|
|
337
|
+
Parameters:
|
|
338
|
+
|
|
339
|
+
|param|type|description|
|
|
340
|
+
|-----|----|-----------|
|
|
341
|
+
|`set1`|`ImmutableSet<a>`|The first set to combine|
|
|
342
|
+
|`set2`|`ImmutableSet<a>`|The second set to combine|
|
|
343
|
+
|
|
344
|
+
Returns:
|
|
345
|
+
|
|
346
|
+
|type|description|
|
|
347
|
+
|----|-----------|
|
|
348
|
+
|`ImmutableSet<a>`|A set containing only shared elements from both sets|
|
|
349
|
+
|
|
350
|
+
### ImmutableSet.**fromList**
|
|
351
|
+
|
|
352
|
+
<details disabled>
|
|
353
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
354
|
+
No other changes yet.
|
|
355
|
+
</details>
|
|
356
|
+
|
|
357
|
+
```grain
|
|
358
|
+
fromList : List<a> -> ImmutableSet<a>
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Creates a set from a list.
|
|
362
|
+
|
|
363
|
+
Parameters:
|
|
364
|
+
|
|
365
|
+
|param|type|description|
|
|
366
|
+
|-----|----|-----------|
|
|
367
|
+
|`list`|`List<a>`|The list to convert|
|
|
368
|
+
|
|
369
|
+
Returns:
|
|
370
|
+
|
|
371
|
+
|type|description|
|
|
372
|
+
|----|-----------|
|
|
373
|
+
|`ImmutableSet<a>`|A set containing all list values|
|
|
374
|
+
|
|
375
|
+
### ImmutableSet.**toList**
|
|
376
|
+
|
|
377
|
+
<details disabled>
|
|
378
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
379
|
+
No other changes yet.
|
|
380
|
+
</details>
|
|
381
|
+
|
|
382
|
+
```grain
|
|
383
|
+
toList : ImmutableSet<a> -> List<a>
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
Converts a set into a list of its elements.
|
|
387
|
+
|
|
388
|
+
Parameters:
|
|
389
|
+
|
|
390
|
+
|param|type|description|
|
|
391
|
+
|-----|----|-----------|
|
|
392
|
+
|`set`|`ImmutableSet<a>`|The set to convert|
|
|
393
|
+
|
|
394
|
+
Returns:
|
|
395
|
+
|
|
396
|
+
|type|description|
|
|
397
|
+
|----|-----------|
|
|
398
|
+
|`List<a>`|A list containing all set values|
|
|
399
|
+
|
|
400
|
+
### ImmutableSet.**fromArray**
|
|
401
|
+
|
|
402
|
+
<details disabled>
|
|
403
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
404
|
+
No other changes yet.
|
|
405
|
+
</details>
|
|
406
|
+
|
|
407
|
+
```grain
|
|
408
|
+
fromArray : Array<a> -> ImmutableSet<a>
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
Creates a set from an array.
|
|
412
|
+
|
|
413
|
+
Parameters:
|
|
414
|
+
|
|
415
|
+
|param|type|description|
|
|
416
|
+
|-----|----|-----------|
|
|
417
|
+
|`array`|`Array<a>`|The array to convert|
|
|
418
|
+
|
|
419
|
+
Returns:
|
|
420
|
+
|
|
421
|
+
|type|description|
|
|
422
|
+
|----|-----------|
|
|
423
|
+
|`ImmutableSet<a>`|A set containing all array values|
|
|
424
|
+
|
|
425
|
+
### ImmutableSet.**toArray**
|
|
426
|
+
|
|
427
|
+
<details disabled>
|
|
428
|
+
<summary tabindex="-1">Added in <code>0.5.4</code></summary>
|
|
429
|
+
No other changes yet.
|
|
430
|
+
</details>
|
|
431
|
+
|
|
432
|
+
```grain
|
|
433
|
+
toArray : ImmutableSet<a> -> Array<a>
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
Converts a set into an array of its elements.
|
|
437
|
+
|
|
438
|
+
Parameters:
|
|
439
|
+
|
|
440
|
+
|param|type|description|
|
|
441
|
+
|-----|----|-----------|
|
|
442
|
+
|`set`|`ImmutableSet<a>`|The set to convert|
|
|
443
|
+
|
|
444
|
+
Returns:
|
|
445
|
+
|
|
446
|
+
|type|description|
|
|
447
|
+
|----|-----------|
|
|
448
|
+
|`Array<a>`|An array containing all set values|
|
|
449
|
+
|
package/list.gr
CHANGED
|
@@ -520,8 +520,8 @@ export let part = (count, list) => {
|
|
|
520
520
|
* @since v0.1.0
|
|
521
521
|
*/
|
|
522
522
|
export let rotate = (count, list) => {
|
|
523
|
-
let (beginning, end) =
|
|
524
|
-
else part(length(list) + count, list)
|
|
523
|
+
let (beginning, end) =
|
|
524
|
+
if (count >= 0) part(count, list) else part(length(list) + count, list)
|
|
525
525
|
append(end, beginning)
|
|
526
526
|
}
|
|
527
527
|
|
|
@@ -548,6 +548,79 @@ export let unique = list => {
|
|
|
548
548
|
iter(list, [])
|
|
549
549
|
}
|
|
550
550
|
|
|
551
|
+
/**
|
|
552
|
+
* Produces a new list filled with tuples of elements from both given lists.
|
|
553
|
+
* The first tuple will contain the first item of each list, the second tuple
|
|
554
|
+
* will contain the second item of each list, and so on.
|
|
555
|
+
*
|
|
556
|
+
* Calling this function with lists of different sizes will cause the returned
|
|
557
|
+
* list to have the length of the smaller list.
|
|
558
|
+
*
|
|
559
|
+
* @param list1: The list to provide values for the first tuple element
|
|
560
|
+
* @param list2: The list to provide values for the second tuple element
|
|
561
|
+
* @returns The new list containing indexed pairs of `(a, b)`
|
|
562
|
+
*
|
|
563
|
+
* @example List.zip([1, 2, 3], [4, 5, 6]) // [(1, 4), (2, 5), (3, 6)]
|
|
564
|
+
* @example List.zip([1, 2, 3], [4, 5]) // [(1, 4), (2, 5)]
|
|
565
|
+
*
|
|
566
|
+
* @since v0.5.3
|
|
567
|
+
*/
|
|
568
|
+
export let zip = (list1, list2) => {
|
|
569
|
+
let rec zipInner = (list1, list2, acc) => {
|
|
570
|
+
match ((list1, list2)) {
|
|
571
|
+
([first1, ...rest1], [first2, ...rest2]) =>
|
|
572
|
+
zipInner(rest1, rest2, [(first1, first2), ...acc]),
|
|
573
|
+
_ => acc,
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
reverse(zipInner(list1, list2, []))
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Produces a new list filled with elements defined by applying a function on
|
|
581
|
+
* pairs from both given lists. The first element will contain the result of
|
|
582
|
+
* applying the function to the first elements of each list, the second element
|
|
583
|
+
* will contain the result of applying the function to the second elements of
|
|
584
|
+
* each list, and so on.
|
|
585
|
+
*
|
|
586
|
+
* Calling this function with lists of different sizes will cause the returned
|
|
587
|
+
* list to have the length of the smaller list.
|
|
588
|
+
*
|
|
589
|
+
* @param fn: The function to apply to pairs of elements
|
|
590
|
+
* @param list1: The list whose elements will each be passed to the function as the first argument
|
|
591
|
+
* @param list2: The list whose elements will each be passed to the function as the second argument
|
|
592
|
+
* @returns The new list containing elements derived from applying the function to pairs of input list elements
|
|
593
|
+
*
|
|
594
|
+
* @example List.zipWith((a, b) => a + b, [1, 2, 3], [4, 5, 6]) // [5, 7, 9]
|
|
595
|
+
* @example List.zipWith((a, b) => a * b, [1, 2, 3], [4, 5]) // [4, 10]
|
|
596
|
+
*
|
|
597
|
+
* @since v0.5.3
|
|
598
|
+
*/
|
|
599
|
+
export let zipWith = (fn, list1, list2) => {
|
|
600
|
+
let rec zipWithInner = (list1, list2, acc) => {
|
|
601
|
+
match ((list1, list2)) {
|
|
602
|
+
([first1, ...rest1], [first2, ...rest2]) =>
|
|
603
|
+
zipWithInner(rest1, rest2, [fn(first1, first2), ...acc]),
|
|
604
|
+
_ => acc,
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
reverse(zipWithInner(list1, list2, []))
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Produces two lists by splitting apart a list of tuples.
|
|
612
|
+
*
|
|
613
|
+
* @param list: The list of tuples to split
|
|
614
|
+
* @returns An list containing all elements from the first tuple element, and a list containing all elements from the second tuple element
|
|
615
|
+
*
|
|
616
|
+
* @since v0.5.3
|
|
617
|
+
*/
|
|
618
|
+
export let unzip = list => {
|
|
619
|
+
reduceRight(((first, second), (firstUnzipped, secondUnzipped)) => {
|
|
620
|
+
([first, ...firstUnzipped], [second, ...secondUnzipped])
|
|
621
|
+
}, ([], []), list)
|
|
622
|
+
}
|
|
623
|
+
|
|
551
624
|
/**
|
|
552
625
|
* Produces a new list with the specified number of elements removed from
|
|
553
626
|
* the beginning of the input list.
|
package/list.md
CHANGED
|
@@ -819,6 +819,116 @@ Returns:
|
|
|
819
819
|
|----|-----------|
|
|
820
820
|
|`List<a>`|The new list with only unique values|
|
|
821
821
|
|
|
822
|
+
### List.**zip**
|
|
823
|
+
|
|
824
|
+
<details disabled>
|
|
825
|
+
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
826
|
+
No other changes yet.
|
|
827
|
+
</details>
|
|
828
|
+
|
|
829
|
+
```grain
|
|
830
|
+
zip : (List<a>, List<b>) -> List<(a, b)>
|
|
831
|
+
```
|
|
832
|
+
|
|
833
|
+
Produces a new list filled with tuples of elements from both given lists.
|
|
834
|
+
The first tuple will contain the first item of each list, the second tuple
|
|
835
|
+
will contain the second item of each list, and so on.
|
|
836
|
+
|
|
837
|
+
Calling this function with lists of different sizes will cause the returned
|
|
838
|
+
list to have the length of the smaller list.
|
|
839
|
+
|
|
840
|
+
Parameters:
|
|
841
|
+
|
|
842
|
+
|param|type|description|
|
|
843
|
+
|-----|----|-----------|
|
|
844
|
+
|`list1`|`List<a>`|The list to provide values for the first tuple element|
|
|
845
|
+
|`list2`|`List<b>`|The list to provide values for the second tuple element|
|
|
846
|
+
|
|
847
|
+
Returns:
|
|
848
|
+
|
|
849
|
+
|type|description|
|
|
850
|
+
|----|-----------|
|
|
851
|
+
|`List<(a, b)>`|The new list containing indexed pairs of `(a, b)`|
|
|
852
|
+
|
|
853
|
+
Examples:
|
|
854
|
+
|
|
855
|
+
```grain
|
|
856
|
+
List.zip([1, 2, 3], [4, 5, 6]) // [(1, 4), (2, 5), (3, 6)]
|
|
857
|
+
```
|
|
858
|
+
|
|
859
|
+
```grain
|
|
860
|
+
List.zip([1, 2, 3], [4, 5]) // [(1, 4), (2, 5)]
|
|
861
|
+
```
|
|
862
|
+
|
|
863
|
+
### List.**zipWith**
|
|
864
|
+
|
|
865
|
+
<details disabled>
|
|
866
|
+
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
867
|
+
No other changes yet.
|
|
868
|
+
</details>
|
|
869
|
+
|
|
870
|
+
```grain
|
|
871
|
+
zipWith : (((a, b) -> c), List<a>, List<b>) -> List<c>
|
|
872
|
+
```
|
|
873
|
+
|
|
874
|
+
Produces a new list filled with elements defined by applying a function on
|
|
875
|
+
pairs from both given lists. The first element will contain the result of
|
|
876
|
+
applying the function to the first elements of each list, the second element
|
|
877
|
+
will contain the result of applying the function to the second elements of
|
|
878
|
+
each list, and so on.
|
|
879
|
+
|
|
880
|
+
Calling this function with lists of different sizes will cause the returned
|
|
881
|
+
list to have the length of the smaller list.
|
|
882
|
+
|
|
883
|
+
Parameters:
|
|
884
|
+
|
|
885
|
+
|param|type|description|
|
|
886
|
+
|-----|----|-----------|
|
|
887
|
+
|`fn`|`(a, b) -> c`|The function to apply to pairs of elements|
|
|
888
|
+
|`list1`|`List<a>`|The list whose elements will each be passed to the function as the first argument|
|
|
889
|
+
|`list2`|`List<b>`|The list whose elements will each be passed to the function as the second argument|
|
|
890
|
+
|
|
891
|
+
Returns:
|
|
892
|
+
|
|
893
|
+
|type|description|
|
|
894
|
+
|----|-----------|
|
|
895
|
+
|`List<c>`|The new list containing elements derived from applying the function to pairs of input list elements|
|
|
896
|
+
|
|
897
|
+
Examples:
|
|
898
|
+
|
|
899
|
+
```grain
|
|
900
|
+
List.zipWith((a, b) => a + b, [1, 2, 3], [4, 5, 6]) // [5, 7, 9]
|
|
901
|
+
```
|
|
902
|
+
|
|
903
|
+
```grain
|
|
904
|
+
List.zipWith((a, b) => a * b, [1, 2, 3], [4, 5]) // [4, 10]
|
|
905
|
+
```
|
|
906
|
+
|
|
907
|
+
### List.**unzip**
|
|
908
|
+
|
|
909
|
+
<details disabled>
|
|
910
|
+
<summary tabindex="-1">Added in <code>0.5.3</code></summary>
|
|
911
|
+
No other changes yet.
|
|
912
|
+
</details>
|
|
913
|
+
|
|
914
|
+
```grain
|
|
915
|
+
unzip : List<(a, b)> -> (List<a>, List<b>)
|
|
916
|
+
```
|
|
917
|
+
|
|
918
|
+
Produces two lists by splitting apart a list of tuples.
|
|
919
|
+
|
|
920
|
+
Parameters:
|
|
921
|
+
|
|
922
|
+
|param|type|description|
|
|
923
|
+
|-----|----|-----------|
|
|
924
|
+
|`list`|`List<(a, b)>`|The list of tuples to split|
|
|
925
|
+
|
|
926
|
+
Returns:
|
|
927
|
+
|
|
928
|
+
|type|description|
|
|
929
|
+
|----|-----------|
|
|
930
|
+
|`(List<a>, List<b>)`|An list containing all elements from the first tuple element, and a list containing all elements from the second tuple element|
|
|
931
|
+
|
|
822
932
|
### List.**drop**
|
|
823
933
|
|
|
824
934
|
<details disabled>
|
package/map.gr
CHANGED
|
@@ -504,8 +504,7 @@ export let reject = (predicate, map) => {
|
|
|
504
504
|
filter((key, value) => !predicate(key, value), map)
|
|
505
505
|
}
|
|
506
506
|
|
|
507
|
-
// TODO: Should return a Record type instead of a Tuple
|
|
508
|
-
// Waiting on https://github.com/grain-lang/grain/issues/190
|
|
507
|
+
// TODO(#190): Should return a Record type instead of a Tuple
|
|
509
508
|
/**
|
|
510
509
|
* Provides data representing the internal state state of the map.
|
|
511
510
|
*
|