@grain/stdlib 0.4.4 → 0.4.5

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 CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ### [0.4.5](https://www.github.com/grain-lang/grain/compare/stdlib-v0.4.4...stdlib-v0.4.5) (2021-12-31)
4
+
5
+
6
+ ### Features
7
+
8
+ * **stdlib:** Add sign function to Number module ([#1079](https://www.github.com/grain-lang/grain/issues/1079)) ([b6483d5](https://www.github.com/grain-lang/grain/commit/b6483d5046cd1b6b89a717a925594d3b20b05837))
9
+
3
10
  ### [0.4.4](https://www.github.com/grain-lang/grain/compare/stdlib-v0.4.3...stdlib-v0.4.4) (2021-12-11)
4
11
 
5
12
 
package/array.gr CHANGED
@@ -30,7 +30,7 @@ let initPtr = () => {
30
30
  initPtr()
31
31
 
32
32
  @disableGC
33
- let initLength = (length) => {
33
+ let initLength = length => {
34
34
  let length = WasmI32.fromGrain(length)
35
35
  if (WasmI32.eqz(WasmI32.and(length, 1n))) {
36
36
  throw Exception.InvalidArgument("Length argument must be an integer")
@@ -51,8 +51,8 @@ let initLength = (length) => {
51
51
  * @since v0.1.0
52
52
  */
53
53
  @disableGC
54
- export let rec length = (array) => {
55
- let ptr = WasmI32.fromGrain(array: Array<a>)
54
+ export let rec length = array => {
55
+ let ptr = WasmI32.fromGrain(array: (Array<a>))
56
56
  let ret = tagSimpleNumber(WasmI32.load(ptr, _ARRAY_LENGTH_OFFSET))
57
57
  Memory.decRef(ptr)
58
58
  Memory.decRef(WasmI32.fromGrain(length))
@@ -73,13 +73,21 @@ export let rec length = (array) => {
73
73
  * @since v0.1.0
74
74
  */
75
75
  @disableGC
76
- export let rec make /*: (Number, a) -> Array<a>*/ = (length: Number, item: a) => {
76
+ export let rec make /*: (Number, a) -> Array<a>*/ =
77
+ (
78
+ length: Number,
79
+ item: a,
80
+ ) => {
77
81
  let lengthArg = length
78
82
  let length = initLength(length)
79
83
  let byteLength = WasmI32.mul(length, 4n)
80
84
  let array = allocateArray(length)
81
85
  for (let mut i = 0n; WasmI32.ltS(i, byteLength); i = WasmI32.add(i, 4n)) {
82
- WasmI32.store(WasmI32.add(array, i), Memory.incRef(WasmI32.fromGrain(item)), _ARRAY_START_OFFSET)
86
+ WasmI32.store(
87
+ WasmI32.add(array, i),
88
+ Memory.incRef(WasmI32.fromGrain(item)),
89
+ _ARRAY_START_OFFSET
90
+ )
83
91
  }
84
92
  let ret = WasmI32.toGrain(array): (Array<a>)
85
93
  Memory.decRef(WasmI32.fromGrain(lengthArg))
@@ -102,16 +110,24 @@ export let rec make /*: (Number, a) -> Array<a>*/ = (length: Number, item: a) =
102
110
  * @since v0.1.0
103
111
  */
104
112
  @disableGC
105
- export let rec init /*: (Number, Number -> a) -> Array<a>*/ = (length: Number, fn: Number -> a) => {
113
+ export let rec init /*: (Number, Number -> a) -> Array<a>*/ =
114
+ (
115
+ length: Number,
116
+ fn: Number -> a,
117
+ ) => {
106
118
  let length = initLength(length)
107
119
  let byteLength = WasmI32.mul(length, 4n)
108
120
  let array = allocateArray(length)
109
121
  let mut index = 0n
110
122
  for (let mut i = 0n; WasmI32.ltS(i, byteLength); i = WasmI32.add(i, 4n)) {
111
- Memory.incRef(WasmI32.fromGrain(fn));
123
+ Memory.incRef(WasmI32.fromGrain(fn))
112
124
  // [FIXME] This line fails the array/map test suite (#815)
113
125
  //assert !WasmI32.eqz(WasmI32.and(WasmI32.fromGrain(index), 1n)) // must be a simple int for next line to be correct
114
- WasmI32.store(WasmI32.add(array, i), WasmI32.fromGrain(fn(tagSimpleNumber(index))), _ARRAY_START_OFFSET)
126
+ WasmI32.store(
127
+ WasmI32.add(array, i),
128
+ WasmI32.fromGrain(fn(tagSimpleNumber(index))),
129
+ _ARRAY_START_OFFSET
130
+ )
115
131
  //WasmI32.store(WasmI32.add(array, i), WasmI32.fromGrain(4), _ARRAY_START_OFFSET)
116
132
  index = WasmI32.add(index, 1n)
117
133
  }
@@ -186,29 +202,29 @@ export let append = (array1, array2) => {
186
202
  *
187
203
  * @since v0.1.0
188
204
  */
189
- export let concat = (arrays) => {
205
+ export let concat = arrays => {
190
206
  // This function is slightly verbose to avoid depending on the List stdlib.
191
207
 
192
208
  let rec findLength = (arrays, acc) => {
193
209
  match (arrays) {
194
210
  [fst, ...rest] => findLength(rest, acc + length(fst)),
195
- [] => acc
211
+ [] => acc,
196
212
  }
197
213
  }
198
214
 
199
215
  let mut offset = 0
200
216
  let mut arrs = arrays
201
217
 
202
- let rec next = (index) => {
218
+ let rec next = index => {
203
219
  let array = match (arrs) {
204
220
  [fst, ..._] => fst,
205
- [] => fail "end of arrays list"
221
+ [] => fail "end of arrays list",
206
222
  }
207
223
  if (index - offset == length(array)) {
208
224
  offset += length(array)
209
225
  arrs = match (arrs) {
210
226
  [_, ...rest] => rest,
211
- [] => fail "end of arrays list"
227
+ [] => fail "end of arrays list",
212
228
  }
213
229
  next(index)
214
230
  } else {
@@ -228,7 +244,7 @@ export let concat = (arrays) => {
228
244
  *
229
245
  * @since v0.1.0
230
246
  */
231
- export let copy = (array) => {
247
+ export let copy = array => {
232
248
  init(length(array), n => array[n])
233
249
  }
234
250
 
@@ -245,7 +261,7 @@ export let cycle = (fn, n, array) => {
245
261
  let length = length(array)
246
262
  for (let mut iteration = 0; iteration < n; iteration += 1) {
247
263
  for (let mut count = 0; count < length; count += 1) {
248
- fn(array[count]): (Void)
264
+ fn(array[count]): Void
249
265
  }
250
266
  }
251
267
  }
@@ -262,7 +278,7 @@ export let cycle = (fn, n, array) => {
262
278
  export let forEach = (fn, array) => {
263
279
  let length = length(array)
264
280
  for (let mut count = 0; count < length; count += 1) {
265
- fn(array[count]): (Void)
281
+ fn(array[count]): Void
266
282
  }
267
283
  }
268
284
 
@@ -279,7 +295,7 @@ export let forEach = (fn, array) => {
279
295
  export let forEachi = (fn, array) => {
280
296
  let length = length(array)
281
297
  for (let mut count = 0; count < length; count += 1) {
282
- fn(array[count], count): (Void)
298
+ fn(array[count], count): Void
283
299
  }
284
300
  }
285
301
 
@@ -296,7 +312,7 @@ export let forEachi = (fn, array) => {
296
312
  */
297
313
  export let map = (fn, array) => {
298
314
  let length = length(array)
299
- init(length, (i) => {
315
+ init(length, i => {
300
316
  fn(array[i])
301
317
  })
302
318
  }
@@ -313,7 +329,7 @@ export let map = (fn, array) => {
313
329
  */
314
330
  export let mapi = (fn, array) => {
315
331
  let length = length(array)
316
- init(length, (index) => {
332
+ init(length, index => {
317
333
  fn(array[index], index)
318
334
  })
319
335
  }
@@ -338,7 +354,7 @@ export let mapi = (fn, array) => {
338
354
  */
339
355
  export let reduce = (fn, initial, array) => {
340
356
  let mut acc = initial
341
- forEach((el) => acc = fn(acc, el), array)
357
+ forEach(el => acc = fn(acc, el), array)
342
358
  acc
343
359
  }
344
360
 
@@ -392,7 +408,9 @@ export let flatMap = (fn, array) => {
392
408
  * @since v0.3.0
393
409
  */
394
410
  export let every = (fn, array) => {
395
- reduce((acc, value) => { acc && fn(value) }, true, array)
411
+ reduce((acc, value) => {
412
+ acc && fn(value)
413
+ }, true, array)
396
414
  }
397
415
 
398
416
  /**
@@ -406,7 +424,9 @@ export let every = (fn, array) => {
406
424
  * @since v0.3.0
407
425
  */
408
426
  export let some = (fn, array) => {
409
- reduce((acc, value) => { acc || fn(value) }, false, array)
427
+ reduce((acc, value) => {
428
+ acc || fn(value)
429
+ }, false, array)
410
430
  }
411
431
 
412
432
  /**
@@ -467,9 +487,9 @@ export let fillRange = (value, start, stop, array) => {
467
487
  *
468
488
  * @since v0.4.0
469
489
  */
470
- export let reverse = (array) => {
490
+ export let reverse = array => {
471
491
  let len = length(array)
472
- init(len, (index) => {
492
+ init(len, index => {
473
493
  let last = len - index - 1
474
494
  array[last]
475
495
  })
@@ -483,7 +503,7 @@ export let reverse = (array) => {
483
503
  *
484
504
  * @since v0.1.0
485
505
  */
486
- export let toList = (array) => {
506
+ export let toList = array => {
487
507
  let rec buildList = (acc, index) => {
488
508
  let index = index - 1
489
509
  if (index < 0) {
@@ -503,22 +523,22 @@ export let toList = (array) => {
503
523
  *
504
524
  * @since v0.1.0
505
525
  */
506
- export let fromList = (list) => {
526
+ export let fromList = list => {
507
527
  let rec listLength = (list, acc) => {
508
528
  match (list) {
509
529
  [_, ...rest] => listLength(rest, acc + 1),
510
- [] => acc
530
+ [] => acc,
511
531
  }
512
532
  }
513
533
 
514
534
  let mut lst = list
515
- let rec next = (index) => {
535
+ let rec next = index => {
516
536
  match (lst) {
517
537
  [fst, ...rest] => {
518
538
  lst = rest
519
539
  fst
520
540
  },
521
- [] => next(index)
541
+ [] => next(index),
522
542
  }
523
543
  }
524
544
  init(listLength(list, 0), next)
@@ -555,14 +575,14 @@ export let contains = (search, array) => {
555
575
  */
556
576
  export let find = (fn, array) => {
557
577
  let length = length(array)
558
- if(length == 0){
578
+ if (length == 0) {
559
579
  None
560
580
  } else {
561
581
  let mut count = 0
562
582
  let mut matching = false
563
583
  let mut matchedItem = array[0]
564
584
  while (count < length) {
565
- if(fn(array[count])) {
585
+ if (fn(array[count])) {
566
586
  matching = true
567
587
  matchedItem = array[count]
568
588
  count = length
@@ -570,7 +590,7 @@ export let find = (fn, array) => {
570
590
  count += 1
571
591
  }
572
592
  }
573
- if(!matching) {
593
+ if (!matching) {
574
594
  None
575
595
  } else {
576
596
  Some(matchedItem)
@@ -589,14 +609,14 @@ export let find = (fn, array) => {
589
609
  */
590
610
  export let findIndex = (fn, array) => {
591
611
  let length = length(array)
592
- if(length == 0){
612
+ if (length == 0) {
593
613
  None
594
614
  } else {
595
615
  let mut count = 0
596
616
  let mut matching = false
597
617
  let mut matchedIndex = 0
598
618
  while (count < length) {
599
- if(fn(array[count])) {
619
+ if (fn(array[count])) {
600
620
  matching = true
601
621
  matchedIndex = count
602
622
  count = length
@@ -604,7 +624,7 @@ export let findIndex = (fn, array) => {
604
624
  count += 1
605
625
  }
606
626
  }
607
- if(!matching) {
627
+ if (!matching) {
608
628
  None
609
629
  } else {
610
630
  Some(matchedIndex)
@@ -628,7 +648,7 @@ export let product = (array1: Array<a>, array2: Array<b>) => {
628
648
  let mut indexA = -1
629
649
 
630
650
  init(lenA * lenB, n => {
631
- if(n % lenB == 0){
651
+ if (n % lenB == 0) {
632
652
  indexA += 1
633
653
  } else {
634
654
  indexA = indexA
@@ -651,7 +671,7 @@ export let count = (fn, array) => {
651
671
  let mut position = 0
652
672
  let mut count = 0
653
673
  for (let mut position = 0; position < length; position += 1) {
654
- if(fn(array[position])) {
674
+ if (fn(array[position])) {
655
675
  count += 1
656
676
  }
657
677
  }
@@ -693,13 +713,13 @@ export let counti = (fn, array) => {
693
713
  export let filter = (fn, array) => {
694
714
  let filtered = copy(array)
695
715
  let mut position = 0
696
- forEach((el) => {
716
+ forEach(el => {
697
717
  if (fn(el)) {
698
718
  filtered[position] = el
699
719
  position += 1
700
720
  }
701
721
  }, array)
702
- init(position, (index) => {
722
+ init(position, index => {
703
723
  filtered[index]
704
724
  })
705
725
  }
@@ -724,7 +744,7 @@ export let filteri = (fn, array) => {
724
744
  position += 1
725
745
  }
726
746
  }, array)
727
- init(position, (index) => {
747
+ init(position, index => {
728
748
  filtered[index]
729
749
  })
730
750
  }
@@ -738,11 +758,9 @@ export let filteri = (fn, array) => {
738
758
  *
739
759
  * @since v0.3.0
740
760
  */
741
- export let unique = (array) => {
742
- filteri(
743
- (el, index) => findIndex(value => value == el, array) == Some(index),
744
- array
745
- )
761
+ export let unique = array => {
762
+ filteri((el, index) =>
763
+ findIndex(value => value == el, array) == Some(index), array)
746
764
  }
747
765
 
748
766
  /**
@@ -777,15 +795,17 @@ export let zip = (array1: Array<a>, array2: Array<b>) => {
777
795
  *
778
796
  * @since v0.4.0
779
797
  */
780
- export let unzip = (array) => {
798
+ export let unzip = array => {
781
799
  let lenArr = length(array)
782
800
 
783
801
  let a = init(lenArr, n => {
784
- let (fst, _) = array[n]; fst
802
+ let (fst, _) = array[n]
803
+ fst
785
804
  })
786
805
 
787
806
  let b = init(lenArr, n => {
788
- let (_, snd) = array[n]; snd
807
+ let (_, snd) = array[n]
808
+ snd
789
809
  })
790
810
 
791
811
  (a, b)
@@ -804,10 +824,10 @@ export let join = (separator: String, items: Array<String>) => {
804
824
  let iter = (acc, str) => {
805
825
  match (acc) {
806
826
  None => Some(str),
807
- Some(prev) => Some(prev ++ separator ++ str)
827
+ Some(prev) => Some(prev ++ separator ++ str),
808
828
  }
809
829
  }
810
- match(reduce(iter, None, items)) {
830
+ match (reduce(iter, None, items)) {
811
831
  None => "",
812
832
  Some(s) => s,
813
833
  }
@@ -850,7 +870,7 @@ export let slice = (startIndex, endIndex, array) => {
850
870
  endIndex
851
871
  }
852
872
 
853
- let newLength = endIndex - startIndex;
873
+ let newLength = endIndex - startIndex
854
874
  if (newLength < 0) {
855
875
  [>]
856
876
  } else if (newLength > arrayLength) {
@@ -872,8 +892,8 @@ export let sort = (comp, array) => {
872
892
  let partition = (low, high) => {
873
893
  let pivot = array[high]
874
894
  let mut i = low - 1
875
- for(let mut j = low; j < high; j += 1) {
876
- if(comp(array[j], pivot) < 0){
895
+ for (let mut j = low; j < high; j += 1) {
896
+ if (comp(array[j], pivot) < 0) {
877
897
  i += 1
878
898
  let temp = array[i]
879
899
  array[i] = array[j]
@@ -886,7 +906,7 @@ export let sort = (comp, array) => {
886
906
  i + 1
887
907
  }
888
908
  let rec quicksort = (low, high) => {
889
- if(low < high){
909
+ if (low < high) {
890
910
  let partitionIndex = partition(low, high)
891
911
  quicksort(partitionIndex + 1, high)
892
912
  quicksort(low, partitionIndex - 1)
@@ -913,8 +933,7 @@ export let rotate = (n, arr) => {
913
933
  let rec gcd = (a, b) => {
914
934
  if (b == 0) {
915
935
  a
916
- }
917
- else{
936
+ } else {
918
937
  gcd(b, a % b)
919
938
  }
920
939
  }
@@ -923,7 +942,7 @@ export let rotate = (n, arr) => {
923
942
  if (arrLen > 0) {
924
943
  let k = n % arrLen
925
944
  let mut d = -1
926
- let mut j = 0
945
+ let mut j = 0
927
946
  for (let mut i = 0; i < gcd(arrLen, k); i += 1) {
928
947
  j = i
929
948
  let temp = arr[i]
package/buffer.gr CHANGED
@@ -16,7 +16,11 @@ import Bytes from "bytes"
16
16
  import String from "string"
17
17
  import { coerceNumberToWasmI32 } from "runtime/numbers"
18
18
 
19
- record Buffer { mut len: Number, initialSize: Number, mut data: Bytes }
19
+ record Buffer {
20
+ mut len: Number,
21
+ initialSize: Number,
22
+ mut data: Bytes,
23
+ }
20
24
 
21
25
  @disableGC
22
26
  let mut _SIZE_OFFSET = 1n
@@ -130,9 +134,8 @@ let addInt32help = (value, buffer) => {
130
134
  * @since v0.4.0
131
135
  */
132
136
  export let make = initialSize => {
133
- if (initialSize < 0) throw Exception.InvalidArgument(
134
- "Buffers size must be >= 0",
135
- )
137
+ if (initialSize < 0)
138
+ throw Exception.InvalidArgument("Buffers size must be >= 0")
136
139
  { len: 0, initialSize, data: Bytes.make(initialSize) }
137
140
  }
138
141
 
package/bytes.gr CHANGED
@@ -15,7 +15,7 @@ import Conv from "runtime/unsafe/conv"
15
15
  import {
16
16
  tagSimpleNumber,
17
17
  allocateBytes,
18
- allocateString
18
+ allocateString,
19
19
  } from "runtime/dataStructures"
20
20
  import Exception from "runtime/exception"
21
21
  import Int32 from "int32"
@@ -85,7 +85,7 @@ let getSize = ptr => WasmI32.load(ptr, _SIZE_OFFSET)
85
85
  @disableGC
86
86
  export let rec make = (size: Number) => {
87
87
  let bytes = allocateBytes(coerceNumberToWasmI32(size))
88
- let ret = WasmI32.toGrain(bytes): (Bytes)
88
+ let ret = WasmI32.toGrain(bytes): Bytes
89
89
  Memory.decRef(WasmI32.fromGrain(size))
90
90
  Memory.decRef(WasmI32.fromGrain(make))
91
91
  ret
@@ -113,7 +113,7 @@ export let rec fromString = (string: String) => {
113
113
  let size = getSize(src)
114
114
  let dst = allocateBytes(size)
115
115
  Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
116
- let ret = WasmI32.toGrain(dst): (Bytes)
116
+ let ret = WasmI32.toGrain(dst): Bytes
117
117
  Memory.decRef(WasmI32.fromGrain(string))
118
118
  Memory.decRef(WasmI32.fromGrain(fromString))
119
119
  ret
@@ -134,7 +134,7 @@ export let rec toString = (bytes: Bytes) => {
134
134
  let size = getSize(src)
135
135
  let dst = allocateString(size)
136
136
  Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
137
- let ret = WasmI32.toGrain(dst): (String)
137
+ let ret = WasmI32.toGrain(dst): String
138
138
  Memory.decRef(WasmI32.fromGrain(bytes))
139
139
  Memory.decRef(WasmI32.fromGrain(toString))
140
140
  ret
@@ -172,7 +172,7 @@ export let rec copy = (b: Bytes) => {
172
172
  let size = getSize(src)
173
173
  let dst = allocateBytes(size)
174
174
  Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET, size)
175
- let ret = WasmI32.toGrain(dst): (Bytes)
175
+ let ret = WasmI32.toGrain(dst): Bytes
176
176
  Memory.decRef(WasmI32.fromGrain(b))
177
177
  Memory.decRef(WasmI32.fromGrain(copy))
178
178
  ret
@@ -200,13 +200,13 @@ export let rec slice = (start: Number, length: Number, bytes: Bytes) => {
200
200
  let length = coerceNumberToWasmI32(length)
201
201
  if (start + length > size) {
202
202
  throw Exception.InvalidArgument(
203
- "The given index and length do not specify a valid range of bytes",
203
+ "The given index and length do not specify a valid range of bytes"
204
204
  )
205
205
  }
206
206
  let dst = allocateBytes(length)
207
207
  let offset = start
208
208
  Memory.copy(dst + _VALUE_OFFSET, src + _VALUE_OFFSET + start, length)
209
- let ret = WasmI32.toGrain(dst): (Bytes)
209
+ let ret = WasmI32.toGrain(dst): Bytes
210
210
  Memory.decRef(WasmI32.fromGrain(iOrig))
211
211
  Memory.decRef(WasmI32.fromGrain(lenOrig))
212
212
  Memory.decRef(WasmI32.fromGrain(bytes))
@@ -263,10 +263,10 @@ export let rec resize = (left: Number, right: Number, bytes: Bytes) => {
263
263
  Memory.copy(
264
264
  dst + _VALUE_OFFSET + dstOffset,
265
265
  src + _VALUE_OFFSET + srcOffset,
266
- len,
266
+ len
267
267
  )
268
268
  }
269
- let ret = WasmI32.toGrain(dst): (Bytes)
269
+ let ret = WasmI32.toGrain(dst): Bytes
270
270
  Memory.decRef(WasmI32.fromGrain(leftOrig))
271
271
  Memory.decRef(WasmI32.fromGrain(rightOrig))
272
272
  Memory.decRef(WasmI32.fromGrain(bytes))
@@ -317,7 +317,7 @@ export let rec move =
317
317
  let ret = Memory.copy(
318
318
  dst + _VALUE_OFFSET + dstIndex,
319
319
  src + _VALUE_OFFSET + srcIndex,
320
- length,
320
+ length
321
321
  )
322
322
 
323
323
  Memory.decRef(WasmI32.fromGrain(srcIndexOrig))