1
1
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2
- *
2
+ *
3
3
* This program is free software: you can redistribute it and/or modify
4
4
* it under the terms of the GNU Lesser General Public License as published by
5
5
* the Free Software Foundation, either version 3 of the License, or
17
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
19
* GNU Lesser General Public License for more details.
20
- *
20
+ *
21
21
* You should have received a copy of the GNU Lesser General Public License
22
22
* along with this program; if not, write to the Free Software
23
23
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24
24
25
25
26
- (* * A stdlib shipped with BuckleScript
26
+ (* * A stdlib shipped with BuckleScript
27
27
28
- This stdlib is still in {i beta} status, but we encourage you to try it out and
28
+ This stdlib is still in {i beta} status, but we encourage you to try it out and
29
29
provide feedback.
30
30
31
31
{b Motivation }
32
32
33
- The motivation of creating such library is to provide BuckleScript users a
34
- better end-to-end user experience, since the original OCaml stdlib was not
35
- written with JS platform in mind, below are a list of areas this lib aims to
33
+ The motivation of creating such library is to provide BuckleScript users a
34
+ better end-to-end user experience, since the original OCaml stdlib was not
35
+ written with JS platform in mind, below are a list of areas this lib aims to
36
36
improve: {ol
37
37
{- 1. Consistency in name convention: camlCase, and arguments order}
38
38
{- 2. Exception thrown functions are all suffixed with {i Exn}, e.g, {i getExn}}
55
55
{b A special encoding for collection safety}
56
56
57
57
When we create a collection library for a custom data type, take {i Set} for
58
- example, suppose its element type is a pair of ints,
58
+ example, suppose its element type is a pair of ints,
59
59
it needs a custom {i compare} function. However, the {i Set} could not
60
60
just be typed as [ Set.t (int * int) ],
61
61
its customized {i compare} function needs to be
68
68
We introduced a phantom type to solve the problem
69
69
70
70
{[
71
- type t = int * int
72
- module I0 =
73
- (val Belt.Id.comparableU (fun[\@bs] ((a0,a1) : t) ((b0,b1) : t) ->
74
- match compare a0 b0 with
75
- | 0 -> compare a1 b1
76
- | c -> c
77
- ))
78
- let s0 = Belt.Set.make ~id:(module I0)
79
- module I1 =
80
- (val Belt.Id.comparableU (fun[\@bs] ((a0,a1) : t) ((b0,b1) : t) ->
81
- match compare a1 b1 with
82
- | 0 -> compare a0 b0
83
- | c -> c
84
- ))
85
- let s1 = Belt.Set.make ~id:(module I1)
71
+ module Comparable1 = Belt.Id.MakeComparable(struct
72
+ type t = int * int
73
+ let cmp (a0, a1) (b0, b1) =
74
+ match Pervasives.compare a0 b0 with
75
+ | 0 -> Pervasives.compare a1 b1
76
+ | c -> c
77
+ end)
78
+
79
+ let mySet1 = Belt.Set.make ~id:(module Comparable1)
80
+
81
+ module Comparable2 = Belt.Id.MakeComparable(struct
82
+ type t = int * int
83
+ let cmp (a0, a1) (b0, b1) =
84
+ match Pervasives.compare a0 b0 with
85
+ | 0 -> Pervasives.compare a1 b1
86
+ | c -> c
87
+ end)
88
+
89
+ let mySet2 = Belt.Set.make ~id:(module Comparable2)
86
90
]}
87
91
88
- Here the compiler would infer [s0 ] and [s1 ] having different type so that
89
- it would not mix .
92
+ Here, the compiler would infer [mySet1 ] and [mySet2 ] having different type, so
93
+ e.g. a `merge` operation that tries to merge these two sets will correctly fail .
90
94
91
95
{[
92
- val s0 : ((int * int), I0 .identity) t
93
- val s1 : ((int * int), I1 .identity) t
96
+ val mySet1 : ((int * int), Comparable1 .identity) t
97
+ val mySet2 : ((int * int), Comparable2 .identity) t
94
98
]}
95
99
96
- [I0 .identity] and [I1 .identity] are not the same using our encoding scheme.
100
+ [Comparable1 .identity] and [Comparable2 .identity] are not the same using our encoding scheme.
97
101
98
102
{b Collection Hierarchy}
99
103
113
117
technical reasons,
114
118
we {b strongly recommend} users stick to qualified import, {i Belt.Sort}, we may hide
115
119
the internal, {i i.e}, {i Belt_Set} in the future
116
-
120
+
117
121
*)
118
122
119
123
(* * {!Belt.Id}
120
124
121
- Provide utilities to create identified comparators or hashes for
122
- data structures used below.
123
-
125
+ Provide utilities to create identified comparators or hashes for
126
+ data structures used below.
127
+
124
128
It create a unique identifier per module of
125
- functions so that different data structures with slightly different
129
+ functions so that different data structures with slightly different
126
130
comparison functions won't mix
127
131
*)
128
132
module Id = Belt_Id
@@ -134,34 +138,34 @@ module Id = Belt_Id
134
138
module Array = Belt_Array
135
139
136
140
(* * {!Belt.SortArray}
137
-
141
+
138
142
The top level provides some generic sort related utilities.
139
-
143
+
140
144
It also has two specialized inner modules
141
145
{!Belt.SortArray.Int} and {!Belt.SortArray.String}
142
146
*)
143
147
module SortArray = Belt_SortArray
144
148
145
149
(* * {!Belt.MutableQueue}
146
-
150
+
147
151
An FIFO(first in first out) queue data structure
148
152
*)
149
153
module MutableQueue = Belt_MutableQueue
150
154
151
155
(* * {!Belt.MutableStack}
152
-
156
+
153
157
An FILO(first in last out) stack data structure
154
158
*)
155
- module MutableStack = Belt_MutableStack
159
+ module MutableStack = Belt_MutableStack
156
160
157
161
(* * {!Belt.List}
158
-
162
+
159
163
Utilities for List data type
160
164
*)
161
165
module List = Belt_List
162
166
163
167
(* * {!Belt.Range}
164
-
168
+
165
169
Utilities for a closed range [(from, start)]
166
170
*)
167
171
module Range = Belt_Range
@@ -183,29 +187,29 @@ module Set = Belt_Set
183
187
(* * {!Belt.Map},
184
188
185
189
The top level provides generic {b immutable} map operations.
186
-
190
+
187
191
It also has three specialized inner modules
188
192
{!Belt.Map.Int} and {!Belt.Map.String}
189
193
190
194
{!Belt.Map.Dict}: This module separate date from function
191
195
which is more verbose but slightly more efficient
192
- *)
196
+ *)
193
197
module Map = Belt_Map
194
198
195
199
196
200
(* * {!Belt.MutableSet}
197
-
201
+
198
202
The top level provides generic {b mutable} set operations.
199
-
203
+
200
204
It also has two specialized inner modules
201
205
{!Belt.MutableSet.Int} and {!Belt.MutableSet.String}
202
206
*)
203
207
module MutableSet = Belt_MutableSet
204
208
205
209
(* * {!Belt.MutableMap}
206
-
210
+
207
211
The top level provides generic {b mutable} map operations.
208
-
212
+
209
213
It also has two specialized inner modules
210
214
{!Belt.MutableMap.Int} and {!Belt.MutableMap.String}
211
215
@@ -214,24 +218,24 @@ module MutableMap = Belt_MutableMap
214
218
215
219
216
220
(* * {!Belt.HashSet}
217
-
221
+
218
222
The top level provides generic {b mutable} hash set operations.
219
-
223
+
220
224
It also has two specialized inner modules
221
225
{!Belt.HashSet.Int} and {!Belt.HashSet.String}
222
226
*)
223
227
module HashSet = Belt_HashSet
224
228
225
229
226
230
(* * {!Belt.HashMap}
227
-
231
+
228
232
The top level provides generic {b mutable} hash map operations.
229
-
233
+
230
234
It also has two specialized inner modules
231
235
{!Belt.HashMap.Int} and {!Belt.HashMap.String}
232
236
*)
233
237
module HashMap = Belt_HashMap
234
-
238
+
235
239
236
240
237
241
0 commit comments