@grain/stdlib 0.6.5 → 0.7.0

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 (139) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/LICENSE +1 -1
  3. package/README.md +2 -2
  4. package/array.gr +55 -7
  5. package/array.md +123 -77
  6. package/bigint.md +30 -30
  7. package/buffer.gr +20 -53
  8. package/buffer.md +47 -47
  9. package/bytes.gr +111 -35
  10. package/bytes.md +111 -32
  11. package/char.gr +201 -99
  12. package/char.md +361 -34
  13. package/exception.gr +11 -11
  14. package/exception.md +26 -1
  15. package/float32.gr +327 -3
  16. package/float32.md +606 -19
  17. package/float64.gr +320 -3
  18. package/float64.md +606 -19
  19. package/fs.gr +1082 -0
  20. package/fs.md +630 -0
  21. package/hash.gr +142 -88
  22. package/hash.md +102 -14
  23. package/int16.md +23 -23
  24. package/int32.gr +25 -4
  25. package/int32.md +65 -30
  26. package/int64.gr +26 -1
  27. package/int64.md +65 -30
  28. package/int8.md +23 -23
  29. package/json.gr +366 -51
  30. package/json.md +418 -2
  31. package/list.gr +387 -49
  32. package/list.md +492 -69
  33. package/map.gr +20 -12
  34. package/map.md +44 -38
  35. package/marshal.gr +41 -40
  36. package/marshal.md +2 -2
  37. package/number.gr +159 -30
  38. package/number.md +215 -38
  39. package/option.md +21 -21
  40. package/package.json +5 -3
  41. package/path.gr +48 -0
  42. package/path.md +103 -12
  43. package/pervasives.gr +2 -2
  44. package/pervasives.md +37 -37
  45. package/priorityqueue.gr +7 -7
  46. package/priorityqueue.md +19 -19
  47. package/queue.gr +183 -29
  48. package/queue.md +296 -40
  49. package/random.md +6 -6
  50. package/range.gr +4 -4
  51. package/range.md +6 -6
  52. package/rational.md +16 -16
  53. package/regex.gr +52 -51
  54. package/regex.md +11 -11
  55. package/result.md +16 -16
  56. package/runtime/atof/common.md +39 -39
  57. package/runtime/atof/decimal.gr +6 -6
  58. package/runtime/atof/decimal.md +8 -8
  59. package/runtime/atof/lemire.gr +5 -5
  60. package/runtime/atof/lemire.md +1 -1
  61. package/runtime/atof/parse.gr +16 -16
  62. package/runtime/atof/parse.md +2 -2
  63. package/runtime/atof/slow.md +1 -1
  64. package/runtime/atof/table.md +2 -2
  65. package/runtime/atoi/parse.gr +3 -3
  66. package/runtime/atoi/parse.md +1 -1
  67. package/runtime/bigint.gr +15 -47
  68. package/runtime/bigint.md +54 -60
  69. package/runtime/compare.gr +2 -2
  70. package/runtime/compare.md +1 -1
  71. package/runtime/dataStructures.md +33 -33
  72. package/runtime/debugPrint.gr +4 -1
  73. package/runtime/debugPrint.md +9 -9
  74. package/runtime/equal.gr +99 -77
  75. package/runtime/equal.md +1 -1
  76. package/runtime/exception.gr +62 -82
  77. package/runtime/exception.md +62 -11
  78. package/runtime/gc.gr +39 -45
  79. package/runtime/gc.md +4 -4
  80. package/runtime/malloc.gr +7 -7
  81. package/runtime/malloc.md +4 -4
  82. package/runtime/math/kernel/cos.gr +70 -0
  83. package/runtime/math/kernel/cos.md +14 -0
  84. package/runtime/math/kernel/sin.gr +65 -0
  85. package/runtime/math/kernel/sin.md +14 -0
  86. package/runtime/math/kernel/tan.gr +136 -0
  87. package/runtime/math/kernel/tan.md +14 -0
  88. package/runtime/math/rempio2.gr +244 -0
  89. package/runtime/math/rempio2.md +14 -0
  90. package/runtime/math/trig.gr +130 -0
  91. package/runtime/math/trig.md +28 -0
  92. package/runtime/math/umuldi.gr +26 -0
  93. package/runtime/math/umuldi.md +14 -0
  94. package/runtime/numberUtils.gr +29 -29
  95. package/runtime/numberUtils.md +12 -12
  96. package/runtime/numbers.gr +373 -381
  97. package/runtime/numbers.md +79 -73
  98. package/runtime/string.gr +37 -105
  99. package/runtime/string.md +3 -9
  100. package/runtime/unsafe/constants.md +24 -24
  101. package/runtime/unsafe/conv.md +13 -13
  102. package/runtime/unsafe/memory.gr +24 -20
  103. package/runtime/unsafe/memory.md +27 -7
  104. package/runtime/unsafe/offsets.gr +36 -0
  105. package/runtime/unsafe/offsets.md +88 -0
  106. package/runtime/unsafe/panic.gr +28 -0
  107. package/runtime/unsafe/panic.md +14 -0
  108. package/runtime/unsafe/tags.md +32 -32
  109. package/runtime/unsafe/wasmf32.md +28 -28
  110. package/runtime/unsafe/wasmf64.md +28 -28
  111. package/runtime/unsafe/wasmi32.md +47 -47
  112. package/runtime/unsafe/wasmi64.md +50 -50
  113. package/runtime/utf8.gr +189 -0
  114. package/runtime/utf8.md +117 -0
  115. package/runtime/wasi.gr +4 -2
  116. package/runtime/wasi.md +138 -138
  117. package/set.gr +18 -11
  118. package/set.md +42 -36
  119. package/stack.gr +171 -2
  120. package/stack.md +297 -15
  121. package/string.gr +352 -557
  122. package/string.md +77 -34
  123. package/uint16.gr +81 -0
  124. package/uint16.md +183 -22
  125. package/uint32.gr +25 -4
  126. package/uint32.md +63 -28
  127. package/uint64.gr +25 -5
  128. package/uint64.md +63 -28
  129. package/uint8.gr +81 -0
  130. package/uint8.md +183 -22
  131. package/uri.gr +57 -53
  132. package/uri.md +11 -12
  133. package/wasi/file.gr +67 -59
  134. package/wasi/file.md +39 -39
  135. package/wasi/process.md +5 -5
  136. package/wasi/random.md +3 -3
  137. package/wasi/time.md +4 -4
  138. package/runtime/utils/printing.gr +0 -60
  139. package/runtime/utils/printing.md +0 -26
package/json.md CHANGED
@@ -547,7 +547,7 @@ No other changes yet.
547
547
  </details>
548
548
 
549
549
  ```grain
550
- toString :
550
+ toString:
551
551
  (?format: FormattingChoices, json: Json) =>
552
552
  Result<String, JsonToStringError>
553
553
  ```
@@ -616,7 +616,7 @@ No other changes yet.
616
616
  </details>
617
617
 
618
618
  ```grain
619
- parse : (str: String) => Result<Json, JsonParseError>
619
+ parse: (str: String) => Result<Json, JsonParseError>
620
620
  ```
621
621
 
622
622
  Parses JSON string into a `Json` data structure.
@@ -644,3 +644,419 @@ assert parse("{\"currency\":\"$\",\"price\":119}") == Ok(
644
644
  )
645
645
  ```
646
646
 
647
+ ## Json.Lenses
648
+
649
+ Utilities for accessing and updating JSON data.
650
+
651
+ <details disabled>
652
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
653
+ No other changes yet.
654
+ </details>
655
+
656
+ ```grain
657
+ let obj = JsonObject([("x", JsonNumber(123))])
658
+ assert get(property("x") ||> number, obj) == Some(123)
659
+ ```
660
+
661
+ ```grain
662
+ let obj = JsonObject([("x", JsonNumber(123))])
663
+ assert set(property("x") ||> number, 321, obj) ==
664
+ Some(JsonObject([("x", JsonNumber(321))]))
665
+ ```
666
+
667
+ ### Types
668
+
669
+ Type declarations included in the Json.Lenses module.
670
+
671
+ #### Json.Lenses.**Lens**
672
+
673
+ <details disabled>
674
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
675
+ No other changes yet.
676
+ </details>
677
+
678
+ ```grain
679
+ record Lens<a, b> {
680
+ get: (subject: a) => Option<b>,
681
+ set: (newValue: b, subject: a) => Option<a>,
682
+ }
683
+ ```
684
+
685
+ A structure which provides functionality for accessing and setting JSON
686
+ data.
687
+
688
+ Fields:
689
+
690
+ |name|type|description|
691
+ |----|----|-----------|
692
+ |`get`|`(subject: a0) => Option<b0>`|A function which reads a value from the subject.|
693
+ |`set`|`(newValue: b0, subject: a0) => Option<a0>`|A function which immutably updates a value in the subject.|
694
+
695
+ ### Values
696
+
697
+ Functions and constants included in the Json.Lenses module.
698
+
699
+ #### Json.Lenses.**get**
700
+
701
+ ```grain
702
+ get: (lens: Lens<a, b>, subject: a) => Option<b>
703
+ ```
704
+
705
+ Reads the value focused on by the given lens from the input data.
706
+
707
+ Parameters:
708
+
709
+ |param|type|description|
710
+ |-----|----|-----------|
711
+ |`lens`|`Lens<a, b>`|The lens to apply to the subject data|
712
+ |`subject`|`a`|The data which will have the lens applied to it|
713
+
714
+ Returns:
715
+
716
+ |type|description|
717
+ |----|-----------|
718
+ |`Option<b>`|`Some(data)` containing the data read by the lens if the lens matches the given data, or `None` if the data cannot be matched to the lens|
719
+
720
+ Examples:
721
+
722
+ ```grain
723
+ assert get(number, JsonNumber(123)) == Some(123)
724
+ ```
725
+
726
+ ```grain
727
+ assert get(string, JsonString("abc")) == Some("abc")
728
+ ```
729
+
730
+ ```grain
731
+ assert get(number, JsonString("abc")) == None
732
+ ```
733
+
734
+ #### Json.Lenses.**set**
735
+
736
+ ```grain
737
+ set: (lens: Lens<a, b>, newValue: b, subject: a) => Option<a>
738
+ ```
739
+
740
+ Sets the value focused on by the given lens from the input data to the
741
+ desired new value.
742
+
743
+ Parameters:
744
+
745
+ |param|type|description|
746
+ |-----|----|-----------|
747
+ |`lens`|`Lens<a, b>`|The lens to apply to the subject data|
748
+ |`newValue`|`b`|The new value to set at the focus of the lens|
749
+ |`subject`|`a`|The data which will have the lens applied to it|
750
+
751
+ Returns:
752
+
753
+ |type|description|
754
+ |----|-----------|
755
+ |`Option<a>`|`Some(data)` containing the new data after the lens substitution if the lens matches the given data, or `None` if the data cannot be matched to the lens|
756
+
757
+ Examples:
758
+
759
+ ```grain
760
+ assert set(number, 123, JsonBoolean(true)) == Some(JsonNumber(123))
761
+ ```
762
+
763
+ ```grain
764
+ assert set(property("a"), JsonNumber(123), JsonObject([("a", JsonNull)])) == Some(JsonObject([("a", JsonNumber(123))]))
765
+ ```
766
+
767
+ ```grain
768
+ assert set(property("a"), JsonNumber(123), JsonBoolean(true)) == None
769
+ ```
770
+
771
+ #### Json.Lenses.**map**
772
+
773
+ ```grain
774
+ map: (lens: Lens<a, b>, fn: (b => b), subject: a) => Option<a>
775
+ ```
776
+
777
+ Updates the value focused on by the given lens from the input data by
778
+ applying a function to it and setting the focus to the result of the function
779
+
780
+ Parameters:
781
+
782
+ |param|type|description|
783
+ |-----|----|-----------|
784
+ |`lens`|`Lens<a, b>`|The lens to apply to the subject data|
785
+ |`fn`|`b => b`|The function to apply to the matched data at the lens if matched|
786
+ |`subject`|`a`|The data which will have the lens applied to it|
787
+
788
+ Returns:
789
+
790
+ |type|description|
791
+ |----|-----------|
792
+ |`Option<a>`|`Some(data)` containing the new data after the lens mapping has been applied if the lens matches the given data, or `None` if the data cannot be matched to the lens|
793
+
794
+ Examples:
795
+
796
+ ```grain
797
+ assert map(number, x => x * 2, JsonNumber(5)) == Some(JsonNumber(10))
798
+ ```
799
+
800
+ ```grain
801
+ assert map(property("x"), x => JsonArray([x, x]), JsonObject([("x", JsonNumber(1))])) ==
802
+ Some(JsonObject([("x", JsonArray([JsonNumber(1), JsonNumber(1)]))]))
803
+ ```
804
+
805
+ ```grain
806
+ assert map(number, x => x * 2, JsonString("abc")) == None
807
+ ```
808
+
809
+ #### Json.Lenses.**json**
810
+
811
+ <details disabled>
812
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
813
+ No other changes yet.
814
+ </details>
815
+
816
+ ```grain
817
+ json: Lens<Json, Json>
818
+ ```
819
+
820
+ A lens whose focus is a JSON value.
821
+
822
+ Examples:
823
+
824
+ ```grain
825
+ assert get(json, JsonString("abc")) == Some(JsonString("abc"))
826
+ ```
827
+
828
+ #### Json.Lenses.**boolean**
829
+
830
+ <details disabled>
831
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
832
+ No other changes yet.
833
+ </details>
834
+
835
+ ```grain
836
+ boolean: Lens<Json, Bool>
837
+ ```
838
+
839
+ A lens whose focus is a JSON boolean value.
840
+
841
+ Examples:
842
+
843
+ ```grain
844
+ assert get(boolean, JsonBoolean(true)) == Some(true)
845
+ ```
846
+
847
+ #### Json.Lenses.**string**
848
+
849
+ <details disabled>
850
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
851
+ No other changes yet.
852
+ </details>
853
+
854
+ ```grain
855
+ string: Lens<Json, String>
856
+ ```
857
+
858
+ A lens whose focus is a JSON string value.
859
+
860
+ Examples:
861
+
862
+ ```grain
863
+ assert get(string, JsonString("abc")) == Some("abc")
864
+ ```
865
+
866
+ #### Json.Lenses.**number**
867
+
868
+ <details disabled>
869
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
870
+ No other changes yet.
871
+ </details>
872
+
873
+ ```grain
874
+ number: Lens<Json, Number>
875
+ ```
876
+
877
+ A lens whose focus is a JSON number value.
878
+
879
+ Examples:
880
+
881
+ ```grain
882
+ assert get(number, JsonNumber(123)) == Some(123)
883
+ ```
884
+
885
+ #### Json.Lenses.**array**
886
+
887
+ <details disabled>
888
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
889
+ No other changes yet.
890
+ </details>
891
+
892
+ ```grain
893
+ array: Lens<Json, List<Json>>
894
+ ```
895
+
896
+ A lens whose focus is a JSON array.
897
+
898
+ Examples:
899
+
900
+ ```grain
901
+ assert get(array, JsonArray([JsonNumber(123)])) == Some([JsonNumber(123)])
902
+ ```
903
+
904
+ #### Json.Lenses.**objectProperties**
905
+
906
+ <details disabled>
907
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
908
+ No other changes yet.
909
+ </details>
910
+
911
+ ```grain
912
+ objectProperties: Lens<Json, List<(String, Json)>>
913
+ ```
914
+
915
+ A lens whose focus is the property pair list of a JSON object.
916
+
917
+ Examples:
918
+
919
+ ```grain
920
+ assert get(objectProperties, JsonObject([("a", JsonNumber(123))])) == Some([("a", JsonNumber(123))])
921
+ ```
922
+
923
+ #### Json.Lenses.**property**
924
+
925
+ <details disabled>
926
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
927
+ No other changes yet.
928
+ </details>
929
+
930
+ ```grain
931
+ property: (propertyName: String) => Lens<Json, Json>
932
+ ```
933
+
934
+ Creates a lens whose focus is a given property of a JSON object.
935
+
936
+ Parameters:
937
+
938
+ |param|type|description|
939
+ |-----|----|-----------|
940
+ |`propertyName`|`String`|The property name of the JSON object to focus on|
941
+
942
+ Returns:
943
+
944
+ |type|description|
945
+ |----|-----------|
946
+ |`Lens<Json, Json>`|A lens whose focus is the given property of a JSON object|
947
+
948
+ Examples:
949
+
950
+ ```grain
951
+ assert get(property("x"), JsonObject([("x", JsonNumber(123))])) == Some(JsonNumber(123))
952
+ ```
953
+
954
+ ```grain
955
+ assert set(property("x"), JsonString("new"), JsonObject([("x", JsonNumber(123))])) ==
956
+ Some(JsonObject([("x", JsonString("new"))]))
957
+ ```
958
+
959
+ #### Json.Lenses.**nullable**
960
+
961
+ <details disabled>
962
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
963
+ No other changes yet.
964
+ </details>
965
+
966
+ ```grain
967
+ nullable: (lens: Lens<Json, a>) => Lens<Json, Option<a>>
968
+ ```
969
+
970
+ Wraps a lens to permit nullable values in addition to the original value
971
+ type of the given lens. During a `get` operation if the lens matches then
972
+ the result will be enclosed in `Some`; if the lens does not match but the
973
+ value focused is null, then the lens will still successfully match and
974
+ `None` will be returned.
975
+
976
+ Examples:
977
+
978
+ ```grain
979
+ assert get(nullable(number), JsonNumber(123)) == Some(Some(123))
980
+ ```
981
+
982
+ ```grain
983
+ assert get(nullable(number), JsonNull) == Some(None)
984
+ ```
985
+
986
+ ```grain
987
+ assert get(nullable(number), JsonString("abc")) == None
988
+ ```
989
+
990
+ ```grain
991
+ assert set(nullable(number), Some(123), JsonString("abc")) == Some(JsonNumber(123))
992
+ ```
993
+
994
+ #### Json.Lenses.**propertyPath**
995
+
996
+ <details disabled>
997
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
998
+ No other changes yet.
999
+ </details>
1000
+
1001
+ ```grain
1002
+ propertyPath: (propertyNames: List<String>) => Lens<Json, Json>
1003
+ ```
1004
+
1005
+ Creates a lens whose focus is a given property path within a JSON object tree.
1006
+
1007
+ Parameters:
1008
+
1009
+ |param|type|description|
1010
+ |-----|----|-----------|
1011
+ |`propertyNames`|`List<String>`|The property path of the JSON object to create a focus on|
1012
+
1013
+ Returns:
1014
+
1015
+ |type|description|
1016
+ |----|-----------|
1017
+ |`Lens<Json, Json>`|A lens whose focus is the given property path of a JSON object|
1018
+
1019
+ Examples:
1020
+
1021
+ ```grain
1022
+ let nestedObj = JsonObject([("a", JsonObject([("b", JsonNumber(123))]))])
1023
+ assert get(propertyPath(["a", "b"]), nestedObj) == Some(JsonNumber(123))
1024
+ ```
1025
+
1026
+ #### Json.Lenses.**(||>)**
1027
+
1028
+ <details disabled>
1029
+ <summary tabindex="-1">Added in <code>0.7.0</code></summary>
1030
+ No other changes yet.
1031
+ </details>
1032
+
1033
+ ```grain
1034
+ (||>): (lens1: Lens<a, b>, lens2: Lens<b, c>) => Lens<a, c>
1035
+ ```
1036
+
1037
+ Reverse lens composition.
1038
+
1039
+ Parameters:
1040
+
1041
+ |param|type|description|
1042
+ |-----|----|-----------|
1043
+ |`lens1`|`Lens<a, b>`|The lens which will be applied first|
1044
+ |`lens2`|`Lens<b, c>`|The lens which will be applied second|
1045
+
1046
+ Returns:
1047
+
1048
+ |type|description|
1049
+ |----|-----------|
1050
+ |`Lens<a, c>`|A lens which combines the two given lenses, passing through the first and then the second|
1051
+
1052
+ Examples:
1053
+
1054
+ ```grain
1055
+ assert get(property("x") ||> number, JsonObject([("x", JsonNumber(123))])) == Some(123)
1056
+ ```
1057
+
1058
+ ```grain
1059
+ assert set(property("x") ||> string, "new", JsonObject([("x", JsonNumber(123))])) ==
1060
+ Some(JsonObject([("x", JsonString("new"))]))
1061
+ ```
1062
+