Skip to content

Commit 41388d0

Browse files
committed
add docs for map
1 parent d6391de commit 41388d0

File tree

6 files changed

+254
-30
lines changed

6 files changed

+254
-30
lines changed

jscomp/others/belt_Map.mli

+243-20
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ module String = Belt_MapString
7676
(** This module seprate identity from data, it is a bit more verbsoe but slightly
7777
more efficient due to the fact that there is no need to pack identity and data back
7878
after each operation
79+
80+
{b Advanced usage only}
7981
*)
8082
module Dict = Belt_MapDict
8183

@@ -120,16 +122,29 @@ val make: id:('k, 'id) id -> ('k, 'a, 'id) t
120122
(** [make ~id]
121123
122124
@example {[
123-
module IntCmp = (val IntCmp.comparable ~cmp:(fun (x:int) y -> Pervasives.comapre x y))
125+
module IntCmp = (val IntCmp.comparable ~cmp:(fun (x:int) y -> Pervasives.comapre x y));;
124126
let s = make ~id:(module IntCmp)
125127
]}
126128
127129
*)
128130

129131

130132
val isEmpty: _ t -> bool
133+
(** [isEmpty s0]
134+
@example {[
135+
module IntCmp = (val IntCmp.comparable ~cmp:(fun (x:int) y -> Pervasives.comapre x y));;
136+
isEmpty (ofArray [|1,"1"|] ~id:(module IntCmp)) = false;;
137+
]}
138+
*)
131139

132-
val has: ('k, 'a, 'id) t -> 'k -> bool
140+
val has: ('k, 'a, 'id) t -> 'k -> bool
141+
(** [has s k]
142+
143+
@example {[
144+
module IntCmp = (val IntCmp.comparable ~cmp:(fun (x:int) y -> Pervasives.comapre x y));;
145+
has (ofArray [|1,"1"|] ~id:(module IntCmp)) 1 = true;;
146+
]}
147+
*)
133148

134149
val cmpU:
135150
('k, 'v, 'id) t ->
@@ -141,6 +156,12 @@ val cmp:
141156
('k, 'v, 'id) t ->
142157
('v -> 'v -> int ) ->
143158
int
159+
(** [cmp s0 s1 vcmp]
160+
161+
Totoal ordering of map given total ordering of value function.
162+
163+
It will compare size first and each element following the order one by one.
164+
*)
144165

145166
val eqU:
146167
('k, 'a, 'id) t ->
@@ -152,23 +173,44 @@ val eq:
152173
('k, 'a, 'id) t ->
153174
('a -> 'a -> bool) ->
154175
bool
155-
(** [eq m1 m2 cmp] tests whether the maps [m1] and [m2] are
176+
(** [eq m1 m2 veq] tests whether the maps [m1] and [m2] are
156177
equal, that is, contain equal keys and associate them with
157-
equal data. [cmp] is the equality predicate used to compare
178+
equal data. [veq] is the equality predicate used to compare
158179
the data associated with the keys. *)
159180

160181
val forEachU: ('k, 'a, 'id) t -> ('k -> 'a -> unit [@bs]) -> unit
161182
val forEach: ('k, 'a, 'id) t -> ('k -> 'a -> unit) -> unit
162183
(** [forEach m f] applies [f] to all bindings in map [m].
163184
[f] receives the 'k as first argument, and the associated value
164185
as second argument. The bindings are passed to [f] in increasing
165-
order with respect to the ordering over the type of the keys. *)
186+
order with respect to the ordering over the type of the keys.
187+
188+
@example {[
189+
module IntCmp =
190+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
191+
192+
let s0 = ofArray ~id:(module IntCmp) [|4,"4";1,"1";2,"2,"3""|];;
193+
let acc = ref [] ;;
194+
forEach s0 (fun k v -> acc := (k,v) :: !acc);;
195+
196+
!acc = [4,"4"; 3,"3"; 2,"2"; 1,"1"]
197+
]}
198+
*)
166199

167200
val reduceU: ('k, 'a, 'id) t -> 'b -> ('b -> 'k -> 'a -> 'b [@bs]) -> 'b
168-
val reduce: ('k, 'a, 'id) t -> 'b -> ('b -> 'k -> 'a -> 'b) -> 'b
201+
val reduce: ('k, 'a, 'id) t -> 'acc -> ('acc -> 'k -> 'a -> 'acc) -> 'acc
169202
(** [reduce m a f] computes [(f kN dN ... (f k1 d1 a)...)],
170203
where [k1 ... kN] are the keys of all bindings in [m]
171-
(in increasing order), and [d1 ... dN] are the associated data. *)
204+
(in increasing order), and [d1 ... dN] are the associated data.
205+
206+
@example {[
207+
module IntCmp =
208+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
209+
210+
let s0 = ofArray ~id:(module IntCmp) [|4,"4";1,"1";2,"2,"3""|];;
211+
reduce s0 [] (fun acc k v -> (k,v) acc ) = [4,"4";3,"3";2,"2";1,"1"];;
212+
]}
213+
*)
172214

173215
val everyU: ('k, 'a, 'id) t -> ('k -> 'a -> bool [@bs]) -> bool
174216
val every: ('k, 'a, 'id) t -> ('k -> 'a -> bool) -> bool
@@ -181,45 +223,192 @@ val some: ('k, 'a, 'id) t -> ('k -> 'a -> bool) -> bool
181223
satisfy the predicate [p]. Order unspecified *)
182224

183225
val size: ('k, 'a, 'id) t -> int
184-
val toList: ('k, 'a, 'id) t -> ('k * 'a) list
185-
(** In increasing order*)
226+
(** [size s]
227+
228+
@example {[
229+
module IntCmp =
230+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
231+
size (ofArray [2,"2"; 2,"1"; 3,"3"] ~id:(module IntCmp)) = 2 ;;
232+
]}
233+
*)
186234
val toArray: ('k, 'a, 'id) t -> ('k * 'a) array
187-
val ofArray: ('k * 'a) array -> id:('k,'id) id -> ('k,'a,'id) t
235+
(** [toArray s]
236+
237+
@example {[
238+
module IntCmp =
239+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
240+
toArray (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) = [1,"1";2,"2";3,"3"]
241+
]}
242+
243+
*)
244+
val toList: ('k, 'a, 'id) t -> ('k * 'a) list
245+
(** In increasing order
246+
247+
{b See} {!toArray}
248+
*)
249+
250+
val ofArray: ('k * 'a) array -> id:('k,'id) id -> ('k,'a,'id) t
251+
(** [ofArray kvs ~id]
252+
@example {[
253+
module IntCmp =
254+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
255+
toArray (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) = [1,"1";2,"2";3,"3"]
256+
]}
257+
*)
188258
val keysToArray: ('k, 'a, 'id) t -> 'k array
259+
(** [keysToArray s]
260+
261+
@example {[
262+
module IntCmp =
263+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
264+
keysToArray (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) =
265+
[|1;2;3|];;
266+
]}
267+
*)
189268
val valuesToArray: ('k, 'a, 'id) t -> 'a array
269+
(** [valuesToArray s]
270+
271+
@example {[
272+
module IntCmp =
273+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
274+
valuesToArray (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) =
275+
[|"1";"2";"3"|];;
276+
]}
277+
278+
*)
279+
190280
val minKey: ('k, _, _) t -> 'k option
281+
(** [minKey s]
282+
@return thte minimum key, None if not exist
283+
*)
284+
191285
val minKeyUndefined: ('k, _, _) t -> 'k Js.undefined
286+
(** {b See} {!minKey}*)
287+
192288
val maxKey: ('k, _, _) t -> 'k option
289+
(** [maxKey s]
290+
@return thte maximum key, None if not exist
291+
*)
292+
193293
val maxKeyUndefined: ('k, _, _) t -> 'k Js.undefined
294+
(** {b See} {!maxKey} *)
295+
194296
val minimum: ('k, 'a, _) t -> ('k * 'a) option
297+
(** [minimum s]
298+
@return thte minimum key value pair, None if not exist
299+
*)
300+
195301
val minUndefined: ('k, 'a, _) t -> ('k * 'a) Js.undefined
302+
(** {b See} {!minimum} *)
303+
196304
val maximum: ('k, 'a, _) t -> ('k * 'a) option
305+
(** [maximum s]
306+
@return thte maximum key value pair, None if not exist
307+
*)
308+
197309
val maxUndefined:('k, 'a, _) t -> ('k * 'a) Js.undefined
310+
(** {b See} {!maximum}
311+
*)
312+
198313
val get: ('k, 'a, 'id) t -> 'k -> 'a option
314+
(** [get s k]
315+
316+
@example {[
317+
module IntCmp =
318+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
319+
get (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) 2 =
320+
Some "2";;
321+
get (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp)) 2 =
322+
None;;
323+
]}
324+
*)
325+
199326
val getUndefined: ('k, 'a, 'id) t -> 'k -> 'a Js.undefined
327+
(** {b See} {!get}
328+
329+
@return [undefined] when not found
330+
*)
200331
val getWithDefault:
201-
('k, 'a, 'id) t -> 'k -> 'a -> 'a
332+
('k, 'a, 'id) t -> 'k -> 'a -> 'a
333+
(** [getWithDefault s k default]
334+
335+
{b See} {!get}
336+
337+
@return [default] when [k] is not found
338+
339+
*)
202340
val getExn: ('k, 'a, 'id) t -> 'k -> 'a
203-
val checkInvariantInternal: _ t -> unit
204-
(**
205-
{b raise} when invariant is not helld
206-
*)
207-
341+
(** [getExn s k]
342+
343+
{b See} {!getExn}
344+
345+
{b raise} when [k] not exist
346+
*)
347+
208348
(****************************************************************************)
209349

210350
val remove: ('k, 'a, 'id) t -> 'k -> ('k, 'a, 'id) t
211-
(** [remove m x] when [x] is not in [m], [m] is returned reference unchanged *)
351+
(** [remove m x] when [x] is not in [m], [m] is returned reference unchanged.
352+
353+
@example {[
354+
module IntCmp =
355+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
356+
357+
let s0 = (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp));;
358+
359+
let s1 = remove s0 1;;
360+
let s2 = remove s1 1;;
361+
s1 == s2 ;;
362+
keysToArray s1 = [|2;3|];;
363+
]}
364+
365+
*)
366+
212367
val removeMany: ('k, 'a, 'id) t -> 'k array -> ('k, 'a, 'id) t
368+
(** [removeMany s xs]
369+
370+
Removing each of [xs] to [s], note unlike {!remove},
371+
the reference of return value might be changed even if none in [xs]
372+
exists [s]
373+
*)
213374

214375
val set:
215376
('k, 'a, 'id) t -> 'k -> 'a -> ('k, 'a, 'id) t
216377
(** [set m x y ] returns a map containing the same bindings as
217378
[m], with a new binding of [x] to [y]. If [x] was already bound
218-
in [m], its previous binding disappears. *)
379+
in [m], its previous binding disappears.
380+
381+
@example {[
382+
module IntCmp =
383+
(val IntCmp.comparableU ~cmp:(fun[\@bs] (x:int) y -> Pervasives.comapre x y));;
384+
385+
let s0 = (ofArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp));;
386+
387+
let s1 = set s0 2 "3";;
388+
389+
valuesToArray s1 = ["1";"3";"3"];;
390+
]}
391+
*)
392+
219393
val updateU: ('k, 'a, 'id) t -> 'k -> ('a option -> 'a option [@bs]) -> ('k, 'a, 'id) t
220394
val update: ('k, 'a, 'id) t -> 'k -> ('a option -> 'a option) -> ('k, 'a, 'id) t
395+
(** [update m x f] returns a map containing the same bindings as
396+
[m], except for the binding of [x].
397+
Depending on the value of
398+
[y] where [y] is [f (get x m)], the binding of [x] is
399+
added, removed or updated. If [y] is [None], the binding is
400+
removed if it exists; otherwise, if [y] is [Some z] then [x]
401+
is associated to [z] in the resulting map.
402+
*)
403+
221404
val mergeMany:
222405
('k, 'a, 'id) t -> ('k * 'a) array -> ('k, 'a, 'id) t
406+
(** [mergeMany s xs]
407+
408+
Adding each of [xs] to [s], note unlike {!add},
409+
the reference of return value might be changed even if all values in [xs]
410+
exist [s]
411+
*)
223412

224413
val mergeU:
225414
('k, 'a, 'id ) t ->
@@ -265,7 +454,7 @@ val partition:
265454
val split:
266455
('k, 'a, 'id) t -> 'k ->
267456
(('k, 'a, 'id) t * ('k, 'a, 'id) t )* 'a option
268-
(** [split x m] returns a triple [(l, data, r)], where
457+
(** [split x m] returns a tuple [(l r), data], where
269458
[l] is the map with all the bindings of [m] whose 'k
270459
is strictly less than [x];
271460
[r] is the map with all the bindings of [m] whose 'k
@@ -284,10 +473,44 @@ val map: ('k, 'a, 'id) t -> ('a -> 'b) -> ('k ,'b,'id ) t
284473

285474
val mapWithKeyU: ('k, 'a, 'id) t -> ('k -> 'a -> 'b [@bs]) -> ('k, 'b, 'id) t
286475
val mapWithKey: ('k, 'a, 'id) t -> ('k -> 'a -> 'b) -> ('k, 'b, 'id) t
476+
(** [mapWithKey m f]
477+
478+
The same as {!map} except that [f] is supplied with one more argument: the key
479+
480+
481+
*)
482+
287483

288-
val getId: ('a, 'b, 'c) t -> ('a, 'c) id
289484

290485
val getData: ('a, 'b, 'c) t -> ('a, 'b, 'c) Belt_MapDict.t
486+
(** [getData s0]
487+
488+
{b Advanced usage only}
489+
490+
@return the raw data (detached from comparator),
491+
but its type is still manifested, so that user can pass identity directly
492+
without boxing
493+
*)
494+
495+
val getId: ('a, 'b, 'c) t -> ('a, 'c) id
496+
(** [getId s0]
497+
498+
{b Advanced usage only}
291499
500+
@return the identity of [s0]
501+
*)
502+
292503
val packIdData: id:('a, 'b) id -> data:('a, 'c, 'b) Belt_MapDict.t -> ('a, 'c, 'b) t
504+
(** [packIdData ~id ~data]
293505
506+
{b Advanced usage only}
507+
508+
@return the packed collection
509+
*)
510+
511+
(**/**)
512+
val checkInvariantInternal: _ t -> unit
513+
(**
514+
{b raise} when invariant is not helld
515+
*)
516+
(**/**)

jscomp/others/belt_MapInt.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ val map: 'a t -> ('a -> 'b) -> 'b t
148148
val mapWithKeyU: 'a t -> (key -> 'a -> 'b [@bs]) -> 'b t
149149
val mapWithKey: 'a t -> (key -> 'a -> 'b) -> 'b t
150150

151-
151+
(**/**)
152152
val checkInvariantInternal: _ t -> unit
153153
(**
154154
{b raise} when invariant is not helld
155155
*)
156-
156+
(**/**)
157157

jscomp/others/belt_MapString.mli

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ val map: 'a t -> ('a -> 'b) -> 'b t
148148
val mapWithKeyU: 'a t -> (key -> 'a -> 'b [@bs]) -> 'b t
149149
val mapWithKey: 'a t -> (key -> 'a -> 'b) -> 'b t
150150

151-
151+
(**/**)
152152
val checkInvariantInternal: _ t -> unit
153153
(**
154154
{b raise} when invariant is not helld
155155
*)
156-
156+
(**/**)
157157

jscomp/others/belt_Set.mli

+2-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ val getData: ('k,'id) t -> ('k,'id) Belt_SetDict.t
443443
@return the raw data (detached from comparator),
444444
but its type is still manifested, so that user can pass identity directly
445445
without boxing
446-
*)
446+
*)
447+
447448
val getId: ('k,'id) t -> ('k,'id) id
448449
(** [getId s0]
449450

0 commit comments

Comments
 (0)