@grain/stdlib 0.4.0 → 0.4.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/hash.md ADDED
@@ -0,0 +1,44 @@
1
+ ---
2
+ title: Hash
3
+ ---
4
+
5
+ Utilities for hashing any value.
6
+
7
+ <details disabled>
8
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
9
+ No other changes yet.
10
+ </details>
11
+
12
+ ```grain
13
+ import Hash from "hash"
14
+ ```
15
+
16
+ ## Values
17
+
18
+ Functions for hashing.
19
+
20
+ ### Hash.**hash**
21
+
22
+ <details disabled>
23
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
24
+ No other changes yet.
25
+ </details>
26
+
27
+ ```grain
28
+ hash : a -> Number
29
+ ```
30
+
31
+ A generic hash function that produces an integer from any value. If `a == b` then `Hash.hash(a) == Hash.hash(b)`.
32
+
33
+ Parameters:
34
+
35
+ |param|type|description|
36
+ |-----|----|-----------|
37
+ |`anything`|`a`|The value to hash|
38
+
39
+ Returns:
40
+
41
+ |type|description|
42
+ |----|-----------|
43
+ |`Number`|A hash for the given value|
44
+
package/list.gr CHANGED
@@ -329,3 +329,57 @@ let join = (separator: String, items: List<String>) => {
329
329
  Some(s) => s,
330
330
  }
331
331
  }
332
+
333
+ /**
334
+ * Reverses the first list and appends the second list to the end.
335
+ *
336
+ * @param list1: The list to reverse
337
+ * @param list2: The list to append
338
+ * @since v0.4.5
339
+ */
340
+ let rec revAppend = (list1, list2) => {
341
+ match (list1) {
342
+ [hd, ...tl] => revAppend(tl, [hd, ...list2]),
343
+ [] => list2
344
+ }
345
+ }
346
+
347
+ /**
348
+ * Sorts the given list based on a given comparator function. The resulting list is sorted in increasing order.
349
+ *
350
+ * Ordering is calculated using a comparator function which takes two list elements and must return 0 if both are equal, a positive number if the first is greater, and a negative number if the first is smaller.
351
+ * @param comp: The comparator function used to indicate sort order
352
+ * @param list: The list to be sorted
353
+ * @since v0.4.5
354
+ */
355
+ let sort = (comp, list) => {
356
+ let rec merge = (left, right, list) => {
357
+ match((left, right)){
358
+ (_, []) => {
359
+ revAppend(list, left)
360
+ },
361
+ ([], _) => {
362
+ revAppend(list, right)
363
+ },
364
+ ([lhd, ...ltl], [rhd, ...rtl]) => {
365
+ if(comp(lhd, rhd) < 0){
366
+ merge(ltl, right, append([lhd],list))
367
+ }else{
368
+ merge(left, rtl, append([rhd], list))
369
+ }
370
+ }
371
+ }
372
+ }
373
+
374
+ let rec mergesort = (list) => {
375
+ if(length(list) <= 1){
376
+ list
377
+ }else{
378
+ let middle = length(list) / 2
379
+ let (left, right) = part(middle, list)
380
+ merge(mergesort(left), mergesort(right), [])
381
+ }
382
+ }
383
+
384
+ mergesort(list)
385
+ }
package/number.gr CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  isFloat,
16
16
  isBoxedNumber
17
17
  } from "runtime/numbers"
18
+ import { parseInt } from "runtime/stringUtils"
18
19
  import { newFloat64, newInt64 } from "runtime/dataStructures"
19
20
  import Tags from "runtime/unsafe/tags"
20
21
 
@@ -76,9 +77,9 @@ export let rec sqrt = (x: Number) => {
76
77
  let x = WasmI32.fromGrain(x)
77
78
  let sqrtd = WasmF64.sqrt(xval)
78
79
  let ret = if (!isFloat(x) && WasmF64.eq(sqrtd, WasmF64.trunc(sqrtd))) {
79
- WasmI32.toGrain(reducedInteger(WasmI64.truncF64S(sqrtd))): Number
80
+ WasmI32.toGrain(reducedInteger(WasmI64.truncF64S(sqrtd))): (Number)
80
81
  } else {
81
- WasmI32.toGrain(newFloat64(sqrtd)): Number
82
+ WasmI32.toGrain(newFloat64(sqrtd)): (Number)
82
83
  }
83
84
  Memory.decRef(WasmI32.fromGrain(x))
84
85
  Memory.decRef(WasmI32.fromGrain(sqrt))
@@ -119,7 +120,7 @@ export let max = (x: Number, y: Number) => if (x > y) x else y
119
120
  export let rec ceil = (x: Number) => {
120
121
  let xval = coerceNumberToWasmF64(x)
121
122
  let ceiling = WasmI64.truncF64S(WasmF64.ceil(xval))
122
- let ret = WasmI32.toGrain(reducedInteger(ceiling)): Number
123
+ let ret = WasmI32.toGrain(reducedInteger(ceiling)): (Number)
123
124
  Memory.decRef(WasmI32.fromGrain(x))
124
125
  Memory.decRef(WasmI32.fromGrain(ceil))
125
126
  ret
@@ -137,7 +138,7 @@ export let rec ceil = (x: Number) => {
137
138
  export let rec floor = (x: Number) => {
138
139
  let xval = coerceNumberToWasmF64(x)
139
140
  let floored = WasmI64.truncF64S(WasmF64.floor(xval))
140
- let ret = WasmI32.toGrain(reducedInteger(floored)): Number
141
+ let ret = WasmI32.toGrain(reducedInteger(floored)): (Number)
141
142
  Memory.decRef(WasmI32.fromGrain(x))
142
143
  Memory.decRef(WasmI32.fromGrain(floor))
143
144
  ret
@@ -155,7 +156,7 @@ export let rec floor = (x: Number) => {
155
156
  export let rec trunc = (x: Number) => {
156
157
  let xval = coerceNumberToWasmF64(x)
157
158
  let trunced = WasmI64.truncF64S(xval)
158
- let ret = WasmI32.toGrain(reducedInteger(trunced)): Number
159
+ let ret = WasmI32.toGrain(reducedInteger(trunced)): (Number)
159
160
  Memory.decRef(WasmI32.fromGrain(x))
160
161
  Memory.decRef(WasmI32.fromGrain(trunc))
161
162
  ret
@@ -173,7 +174,7 @@ export let rec trunc = (x: Number) => {
173
174
  export let rec round = (x: Number) => {
174
175
  let xval = coerceNumberToWasmF64(x)
175
176
  let rounded = WasmI64.truncF64S(WasmF64.nearest(xval))
176
- let ret = WasmI32.toGrain(reducedInteger(rounded)): Number
177
+ let ret = WasmI32.toGrain(reducedInteger(rounded)): (Number)
177
178
  Memory.decRef(WasmI32.fromGrain(x))
178
179
  Memory.decRef(WasmI32.fromGrain(round))
179
180
  ret
@@ -305,3 +306,20 @@ export let rec isInfinite = (x: Number) => {
305
306
  Memory.decRef(WasmI32.fromGrain(isInfinite))
306
307
  ret
307
308
  }
309
+
310
+ /**
311
+ * Parses a string representation of an integer into a `Number` using the
312
+ * specified radix (also known as a number system "base").
313
+ *
314
+ * If the string has a radix prefix (i.e. "0x"/"0X", "0o"/"0O", or "0b"/"0B"
315
+ * for radixes 16, 8, or 2 respectively), the supplied radix is ignored in
316
+ * favor of the prefix. Underscores that appear in the numeric portion of the
317
+ * input are ignored.
318
+ *
319
+ * @param input: The string to parse
320
+ * @param radix: The number system base to use when parsing the input string
321
+ * @returns `Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise
322
+ *
323
+ * @since v0.4.5
324
+ */
325
+ export parseInt
package/number.md CHANGED
@@ -5,7 +5,7 @@ title: Number
5
5
  Utilities for working with numbers.
6
6
 
7
7
  <details disabled>
8
- <summary tabindex="-1">Added in <code>next</code></summary>
8
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
9
9
  No other changes yet.
10
10
  </details>
11
11
 
@@ -16,7 +16,7 @@ import Number from "number"
16
16
  ### Number.**add**
17
17
 
18
18
  <details disabled>
19
- <summary tabindex="-1">Added in <code>next</code></summary>
19
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
20
20
  No other changes yet.
21
21
  </details>
22
22
 
@@ -42,7 +42,7 @@ Returns:
42
42
  ### Number.**sub**
43
43
 
44
44
  <details disabled>
45
- <summary tabindex="-1">Added in <code>next</code></summary>
45
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
46
46
  No other changes yet.
47
47
  </details>
48
48
 
@@ -68,7 +68,7 @@ Returns:
68
68
  ### Number.**mul**
69
69
 
70
70
  <details disabled>
71
- <summary tabindex="-1">Added in <code>next</code></summary>
71
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
72
72
  No other changes yet.
73
73
  </details>
74
74
 
@@ -94,7 +94,7 @@ Returns:
94
94
  ### Number.**div**
95
95
 
96
96
  <details disabled>
97
- <summary tabindex="-1">Added in <code>next</code></summary>
97
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
98
98
  No other changes yet.
99
99
  </details>
100
100
 
@@ -120,7 +120,7 @@ Returns:
120
120
  ### Number.**sqrt**
121
121
 
122
122
  <details disabled>
123
- <summary tabindex="-1">Added in <code>next</code></summary>
123
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
124
124
  No other changes yet.
125
125
  </details>
126
126
 
@@ -145,7 +145,7 @@ Returns:
145
145
  ### Number.**min**
146
146
 
147
147
  <details disabled>
148
- <summary tabindex="-1">Added in <code>next</code></summary>
148
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
149
149
  No other changes yet.
150
150
  </details>
151
151
 
@@ -171,7 +171,7 @@ Returns:
171
171
  ### Number.**max**
172
172
 
173
173
  <details disabled>
174
- <summary tabindex="-1">Added in <code>next</code></summary>
174
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
175
175
  No other changes yet.
176
176
  </details>
177
177
 
@@ -197,7 +197,7 @@ Returns:
197
197
  ### Number.**ceil**
198
198
 
199
199
  <details disabled>
200
- <summary tabindex="-1">Added in <code>next</code></summary>
200
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
201
201
  No other changes yet.
202
202
  </details>
203
203
 
@@ -222,7 +222,7 @@ Returns:
222
222
  ### Number.**floor**
223
223
 
224
224
  <details disabled>
225
- <summary tabindex="-1">Added in <code>next</code></summary>
225
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
226
226
  No other changes yet.
227
227
  </details>
228
228
 
@@ -247,7 +247,7 @@ Returns:
247
247
  ### Number.**trunc**
248
248
 
249
249
  <details disabled>
250
- <summary tabindex="-1">Added in <code>next</code></summary>
250
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
251
251
  No other changes yet.
252
252
  </details>
253
253
 
@@ -272,7 +272,7 @@ Returns:
272
272
  ### Number.**round**
273
273
 
274
274
  <details disabled>
275
- <summary tabindex="-1">Added in <code>next</code></summary>
275
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
276
276
  No other changes yet.
277
277
  </details>
278
278
 
@@ -297,7 +297,7 @@ Returns:
297
297
  ### Number.**abs**
298
298
 
299
299
  <details disabled>
300
- <summary tabindex="-1">Added in <code>next</code></summary>
300
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
301
301
  No other changes yet.
302
302
  </details>
303
303
 
@@ -322,7 +322,7 @@ Returns:
322
322
  ### Number.**neg**
323
323
 
324
324
  <details disabled>
325
- <summary tabindex="-1">Added in <code>next</code></summary>
325
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
326
326
  No other changes yet.
327
327
  </details>
328
328
 
@@ -347,7 +347,7 @@ Returns:
347
347
  ### Number.**isFinite**
348
348
 
349
349
  <details disabled>
350
- <summary tabindex="-1">Added in <code>next</code></summary>
350
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
351
351
  No other changes yet.
352
352
  </details>
353
353
 
@@ -373,7 +373,7 @@ Returns:
373
373
  ### Number.**isNaN**
374
374
 
375
375
  <details disabled>
376
- <summary tabindex="-1">Added in <code>next</code></summary>
376
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
377
377
  No other changes yet.
378
378
  </details>
379
379
 
@@ -399,7 +399,7 @@ Returns:
399
399
  ### Number.**isInfinite**
400
400
 
401
401
  <details disabled>
402
- <summary tabindex="-1">Added in <code>next</code></summary>
402
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
403
403
  No other changes yet.
404
404
  </details>
405
405
 
@@ -422,3 +422,35 @@ Returns:
422
422
  |----|-----------|
423
423
  |`Bool`|`true` if the value is infinite, otherwise `false`|
424
424
 
425
+ ### Number.**parseInt**
426
+
427
+ <details disabled>
428
+ <summary tabindex="-1">Added in <code>next</code></summary>
429
+ No other changes yet.
430
+ </details>
431
+
432
+ ```grain
433
+ parseInt : (String, Number) -> Result<Number, String>
434
+ ```
435
+
436
+ Parses a string representation of an integer into a `Number` using the
437
+ specified radix (also known as a number system "base").
438
+
439
+ If the string has a radix prefix (i.e. "0x"/"0X", "0o"/"0O", or "0b"/"0B"
440
+ for radixes 16, 8, or 2 respectively), the supplied radix is ignored in
441
+ favor of the prefix. Underscores that appear in the numeric portion of the
442
+ input are ignored.
443
+
444
+ Parameters:
445
+
446
+ |param|type|description|
447
+ |-----|----|-----------|
448
+ |`input`|`String`|The string to parse|
449
+ |`radix`|`Number`|The number system base to use when parsing the input string|
450
+
451
+ Returns:
452
+
453
+ |type|description|
454
+ |----|-----------|
455
+ |`Result<Number, String>`|`Ok(value)` containing the parsed number on a successful parse or `Err(msg)` containing an error message string otherwise|
456
+