Skip to content

Commit c9e3bff

Browse files
authored
Merge branch 'master' into master
2 parents 653c87c + bcf4a24 commit c9e3bff

File tree

5 files changed

+277
-504
lines changed

5 files changed

+277
-504
lines changed

jscomp/others/belt.ml

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,22 +17,22 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525

26-
(** A stdlib shipped with BuckleScript
26+
(** A stdlib shipped with BuckleScript
2727
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
2929
provide feedback.
3030
3131
{b Motivation }
3232
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
3636
improve: {ol
3737
{- 1. Consistency in name convention: camlCase, and arguments order}
3838
{- 2. Exception thrown functions are all suffixed with {i Exn}, e.g, {i getExn}}
@@ -55,7 +55,7 @@
5555
{b A special encoding for collection safety}
5656
5757
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,
5959
it needs a custom {i compare} function. However, the {i Set} could not
6060
just be typed as [ Set.t (int * int) ],
6161
its customized {i compare} function needs to be
@@ -68,32 +68,36 @@
6868
We introduced a phantom type to solve the problem
6969
7070
{[
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)
8690
]}
8791
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.
9094
9195
{[
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
9498
]}
9599
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.
97101
98102
{b Collection Hierarchy}
99103
@@ -113,16 +117,16 @@
113117
technical reasons,
114118
we {b strongly recommend} users stick to qualified import, {i Belt.Sort}, we may hide
115119
the internal, {i i.e}, {i Belt_Set} in the future
116-
120+
117121
*)
118122

119123
(** {!Belt.Id}
120124
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+
124128
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
126130
comparison functions won't mix
127131
*)
128132
module Id = Belt_Id
@@ -134,34 +138,34 @@ module Id = Belt_Id
134138
module Array = Belt_Array
135139

136140
(** {!Belt.SortArray}
137-
141+
138142
The top level provides some generic sort related utilities.
139-
143+
140144
It also has two specialized inner modules
141145
{!Belt.SortArray.Int} and {!Belt.SortArray.String}
142146
*)
143147
module SortArray = Belt_SortArray
144148

145149
(** {!Belt.MutableQueue}
146-
150+
147151
An FIFO(first in first out) queue data structure
148152
*)
149153
module MutableQueue = Belt_MutableQueue
150154

151155
(** {!Belt.MutableStack}
152-
156+
153157
An FILO(first in last out) stack data structure
154158
*)
155-
module MutableStack = Belt_MutableStack
159+
module MutableStack = Belt_MutableStack
156160

157161
(** {!Belt.List}
158-
162+
159163
Utilities for List data type
160164
*)
161165
module List = Belt_List
162166

163167
(** {!Belt.Range}
164-
168+
165169
Utilities for a closed range [(from, start)]
166170
*)
167171
module Range = Belt_Range
@@ -183,29 +187,29 @@ module Set = Belt_Set
183187
(** {!Belt.Map},
184188
185189
The top level provides generic {b immutable} map operations.
186-
190+
187191
It also has three specialized inner modules
188192
{!Belt.Map.Int} and {!Belt.Map.String}
189193
190194
{!Belt.Map.Dict}: This module separate date from function
191195
which is more verbose but slightly more efficient
192-
*)
196+
*)
193197
module Map = Belt_Map
194198

195199

196200
(** {!Belt.MutableSet}
197-
201+
198202
The top level provides generic {b mutable} set operations.
199-
203+
200204
It also has two specialized inner modules
201205
{!Belt.MutableSet.Int} and {!Belt.MutableSet.String}
202206
*)
203207
module MutableSet = Belt_MutableSet
204208

205209
(** {!Belt.MutableMap}
206-
210+
207211
The top level provides generic {b mutable} map operations.
208-
212+
209213
It also has two specialized inner modules
210214
{!Belt.MutableMap.Int} and {!Belt.MutableMap.String}
211215
@@ -214,29 +218,29 @@ module MutableMap = Belt_MutableMap
214218

215219

216220
(** {!Belt.HashSet}
217-
221+
218222
The top level provides generic {b mutable} hash set operations.
219-
223+
220224
It also has two specialized inner modules
221225
{!Belt.HashSet.Int} and {!Belt.HashSet.String}
222226
*)
223227
module HashSet = Belt_HashSet
224228

225229

226230
(** {!Belt.HashMap}
227-
231+
228232
The top level provides generic {b mutable} hash map operations.
229-
233+
230234
It also has two specialized inner modules
231235
{!Belt.HashMap.Int} and {!Belt.HashMap.String}
232236
*)
233237
module HashMap = Belt_HashMap
238+
234239

235240
(** {!Belt.Option}
236241
237242
Utilities for option data type
238243
*)
239-
240244
module Option = Belt_Option
241245

242246

0 commit comments

Comments
 (0)