forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBelt_Map.resi
534 lines (411 loc) · 13.4 KB
/
Belt_Map.resi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/* ********************************************************************* */
/* */
/* OCaml */
/* */
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
/* */
/* Copyright 1996 Institut National de Recherche en Informatique et */
/* en Automatique. All rights reserved. This file is distributed */
/* under the terms of the GNU Library General Public License, with */
/* the special exception on linking described in file ../LICENSE. */
/* */
/* Adapted by authors of ReScript without using functors */
/* ********************************************************************* */
/***
The top level provides generic immutable map operations.
It also has three specialized inner modules `Belt.Map.Int`, `Belt.Map.String`
and `Belt.Map.Dict`.
*/
/**
## Examples
```rescript
type t<'key, 'value, 'identity>
type id<'key, 'id> = Belt_Id.comparable<'key, 'id>
```
*/
module Int = Belt_MapInt
module String = Belt_MapString
module Dict = Belt_MapDict
/**
`'key` is the field type
`'value` is the element type
`'identity` the identity of the collection
*/
type t<'key, 'value, 'identity>
/** The identity needed for making an empty map. */
type id<'key, 'id> = Belt_Id.comparable<'key, 'id>
/**
`make(~id)` creates a new map by taking in the comparator.
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
let m = Belt.Map.make(~id=module(IntCmp))
Belt.Map.set(m, 0, "a")
```
*/
let make: (~id: id<'k, 'id>) => t<'k, 'v, 'id>
/**
`isEmpty(m)` checks whether a map m is empty.
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
Belt.Map.isEmpty(Belt.Map.fromArray([(1, "1")], ~id=module(IntCmp))) == false
```
*/
let isEmpty: t<_> => bool
/**
`has(m, k)` checks whether `m` has the key `k`.
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
Belt.Map.has(Belt.Map.fromArray([(1, "1")], ~id=module(IntCmp)), 1) == true
```
*/
let has: (t<'k, 'v, 'id>, 'k) => bool
@deprecated("Use `cmp` instead")
let cmpU: (t<'k, 'v, 'id>, t<'k, 'v, 'id>, ('v, 'v) => int) => int
/**
`cmp(m0, m1, vcmp);`
Total ordering of map given total ordering of value function.
It will compare size first and each element following the order one by one.
*/
let cmp: (t<'k, 'v, 'id>, t<'k, 'v, 'id>, ('v, 'v) => int) => int
@deprecated("Use `eq` instead")
let eqU: (t<'k, 'v, 'id>, t<'k, 'v, 'id>, ('v, 'v) => bool) => bool
/**
eq(m1, m2, veq)` tests whether the maps `m1` and `m2` are equal, that is,
contain equal keys and associate them with equal data. `veq` is the
equality predicate used to compare the data associated with the keys.
*/
let eq: (t<'k, 'v, 'id>, t<'k, 'v, 'id>, ('v, 'v) => bool) => bool
@deprecated("Use `findFirstBy` instead")
let findFirstByU: (t<'k, 'v, 'id>, ('k, 'v) => bool) => option<('k, 'v)>
/** `
findFirstBy(m, p)` uses function `f` to find the first key value pair to match predicate `p`.
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
let s0 = Belt.Map.fromArray(~id=module(IntCmp), [(4, "4"), (1, "1"), (2, "2"), (3, "")])
s0
->Belt.Map.findFirstBy((k, _) => k == 4)
->assertEqual(Some(4, "4"))
```
*/
let findFirstBy: (t<'k, 'v, 'id>, ('k, 'v) => bool) => option<('k, 'v)>
@deprecated("Use `forEach` instead")
let forEachU: (t<'k, 'v, 'id>, ('k, 'v) => unit) => unit
/**
`forEach(m, f)` applies `f` to all bindings in map `m`. `f` receives the
`'k` as first argument, and the associated value as second argument. The
bindings are passed to `f` in increasing order with respect to the ordering
over the type of the keys.
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
let s0 = Belt.Map.fromArray(~id=module(IntCmp), [(4, "4"), (1, "1"), (2, "2"), (3, "")])
let acc = ref(list{})
Belt.Map.forEach(s0, (k, v) => acc := list{(k, v), ...acc.contents})
acc.contents == list{(4, "4"), (3, "3"), (2, "2"), (1, "1")}
```
*/
let forEach: (t<'k, 'v, 'id>, ('k, 'v) => unit) => unit
@deprecated("Use `reduce` instead")
let reduceU: (t<'k, 'v, 'id>, 'acc, ('acc, 'k, 'v) => 'acc) => 'acc
/**
`reduce(m, a, f)` computes `(f(kN, dN) ... (f(k1, d1, a))...)`, where `k1
... kN` are the keys of all bindings in m (in increasing order), and `d1
... dN` are the associated data.
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
let s0 = Belt.Map.fromArray(~id=module(IntCmp), [(4, "4"), (1, "1"), (2, "2"), (3, "3")])
Belt.Map.reduce(s0, list{}, (acc, k, v) => list{
(k, v),
...acc,
}) /* [(4, "4"), (3, "3"), (2, "2"), (1, "1"), 0] */
```
*/
let reduce: (t<'k, 'v, 'id>, 'acc, ('acc, 'k, 'v) => 'acc) => 'acc
@deprecated("Use `every` instead")
let everyU: (t<'k, 'v, 'id>, ('k, 'v) => bool) => bool
/**
`every(m, p)` checks if all the bindings of the map satisfy the predicate
`p`. Order unspecified
*/
let every: (t<'k, 'v, 'id>, ('k, 'v) => bool) => bool
@deprecated("Use `some` instead")
let someU: (t<'k, 'v, 'id>, ('k, 'v) => bool) => bool
/**
`some(m, p)` checks if at least one binding of the map satisfy the predicate
`p`. Order unspecified */
let some: (t<'k, 'v, 'id>, ('k, 'v) => bool) => bool
/**
`size(s)`
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
Belt.Map.size(Belt.Map.fromArray([(2, "2"), (2, "1"), (3, "3")], ~id=module(IntCmp))) == 2
```
*/
let size: t<'k, 'v, 'id> => int
/**
`toArray(s)`
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
Belt.Map.toArray(Belt.Map.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntCmp))) == [
(1, "1"),
(2, "2"),
(3, "3"),
]
```
*/
let toArray: t<'k, 'v, 'id> => array<('k, 'v)>
/** In increasing order. See `Belt.Map.toArray`*/
let toList: t<'k, 'v, 'id> => list<('k, 'v)>
/**
`fromArray(kvs, ~id);`
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
Belt.Map.toArray(Belt.Map.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntCmp))) == [
(1, "1"),
(2, "2"),
(3, "3"),
]
```
*/
let fromArray: (array<('k, 'v)>, ~id: id<'k, 'id>) => t<'k, 'v, 'id>
/**
`keysToArray(s);`
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
Belt.Map.keysToArray(Belt.Map.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntCmp))) == [
1,
2,
3,
]
```
*/
let keysToArray: t<'k, 'v, 'id> => array<'k>
/**
`valuesToArray(s);`
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
Belt.Map.valuesToArray(
Belt.Map.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntCmp)),
) == ["1", "2", "3"]
```
*/
let valuesToArray: t<'k, 'v, 'id> => array<'v>
/** `minKey(s)` returns the minimum key, None if not exist. */
let minKey: t<'k, _, _> => option<'k>
/** See `Belt.Map.minKey` */
let minKeyUndefined: t<'k, _, _> => Js.undefined<'k>
/** `maxKey(s)` returns the maximum key, None if not exist. */
let maxKey: t<'k, _, _> => option<'k>
/** See `Belt.Map.maxKey` */
let maxKeyUndefined: t<'k, _, _> => Js.undefined<'k>
/** `minimum(s)` returns the minimum key value pair, None if not exist. */
let minimum: t<'k, 'v, _> => option<('k, 'v)>
/** See `Belt.Map.minimum` */
let minUndefined: t<'k, 'v, _> => Js.undefined<('k, 'v)>
/** `maximum(s)` returns the maximum key value pair, None if not exist. */
let maximum: t<'k, 'v, _> => option<('k, 'v)>
/** See `Belt.Map.maximum` */
let maxUndefined: t<'k, 'v, _> => Js.undefined<('k, 'v)>
/**
`get(s, k)`
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
Belt.Map.get(Belt.Map.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntCmp)), 2) ==
Some("2")
Belt.Map.get(Belt.Map.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntCmp)), 2) == None
```
*/
let get: (t<'k, 'v, 'id>, 'k) => option<'v>
/** See `Belt.Map.get`. Returns `undefined` when not found*/
let getUndefined: (t<'k, 'v, 'id>, 'k) => Js.undefined<'v>
/**
`getWithDefault(s, k, default)`
See `Belt.Map.get`
Returns default when `k` is not found.
*/
let getWithDefault: (t<'k, 'v, 'id>, 'k, 'v) => 'v
/**
`getExn(s, k)`
See `Belt.Map.getExn`
raise when `k` not exist
*/
let getExn: (t<'k, 'v, 'id>, 'k) => 'v
/* ************************************************************************** */
/**
`remove(m, x)` when `x` is not in `m`, `m` is returned reference unchanged.
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
let s0 = Belt.Map.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntCmp))
let s1 = Belt.Map.remove(s0, 1)
let s2 = Belt.Map.remove(s1, 1)
s1 === s2
Belt.Map.keysToArray(s1) == [2, 3]
```
*/
let remove: (t<'k, 'v, 'id>, 'k) => t<'k, 'v, 'id>
/**
`removeMany(s, xs)`
Removing each of `xs` to `s`, note unlike `Belt.Map.remove`, the reference
of return value might be changed even if none in `xs` exists `s`.
*/
let removeMany: (t<'k, 'v, 'id>, array<'k>) => t<'k, 'v, 'id>
/**
`set(m, x, y)` returns a map containing the same bindings as `m`, with a
new binding of `x` to `y`. If `x` was already bound in `m`, its previous
binding disappears.
## Examples
```rescript
module IntCmp = Belt.Id.MakeComparable({
type t = int
let cmp = (a, b) => Pervasives.compare(a, b)
})
let s0 = Belt.Map.fromArray([(2, "2"), (1, "1"), (3, "3")], ~id=module(IntCmp))
let s1 = Belt.Map.set(s0, 2, "3")
Belt.Map.valuesToArray(s1) == ["1", "3", "3"]
```
*/
let set: (t<'k, 'v, 'id>, 'k, 'v) => t<'k, 'v, 'id>
@deprecated("Use `update` instead")
let updateU: (t<'k, 'v, 'id>, 'k, option<'v> => option<'v>) => t<'k, 'v, 'id>
/**
`update(m, x, f)` returns a map containing the same bindings as `m`, except
for the binding of `x`. Depending on the value of `y` where `y` is
`f(get(m, x))`, the binding of `x` is added, removed or updated. If `y` is
`None`, the binding is removed if it exists; otherwise, if `y` is `Some(z)`
then `x` is associated to `z` in the resulting map.
*/
let update: (t<'k, 'v, 'id>, 'k, option<'v> => option<'v>) => t<'k, 'v, 'id>
/**
`mergeMany(s, xs)`
Adding each of `xs` to `s`, note unlike `add`, the reference of return
value might be changed even if all values in `xs` exist `s`.
*/
let mergeMany: (t<'k, 'v, 'id>, array<('k, 'v)>) => t<'k, 'v, 'id>
@deprecated("Use `merge` instead")
let mergeU: (
t<'k, 'v, 'id>,
t<'k, 'v2, 'id>,
('k, option<'v>, option<'v2>) => option<'v3>,
) => t<'k, 'v3, 'id>
/**
`merge(m1, m2, f)` computes a map whose keys is a subset of keys of `m1`
and of `m2`. The presence of each such binding, and the corresponding
value, is determined with the function `f`.
*/
let merge: (
t<'k, 'v, 'id>,
t<'k, 'v2, 'id>,
('k, option<'v>, option<'v2>) => option<'v3>,
) => t<'k, 'v3, 'id>
@deprecated("Use `keep` instead")
let keepU: (t<'k, 'v, 'id>, ('k, 'v) => bool) => t<'k, 'v, 'id>
/**
`keep(m, p)` returns the map with all the bindings in m that satisfy
predicate `p`.
*/
let keep: (t<'k, 'v, 'id>, ('k, 'v) => bool) => t<'k, 'v, 'id>
@deprecated("Use `partition` instead")
let partitionU: (t<'k, 'v, 'id>, ('k, 'v) => bool) => (t<'k, 'v, 'id>, t<'k, 'v, 'id>)
/**
`partition(m, p)` returns a pair of maps `(m1, m2)`, where `m1` contains
all the bindings of `s` that satisfy the predicate `p`, and `m2` is the map
with all the bindings of `s` that do not satisfy `p`.
*/
let partition: (t<'k, 'v, 'id>, ('k, 'v) => bool) => (t<'k, 'v, 'id>, t<'k, 'v, 'id>)
/**
`split(x, m)` returns a tuple `(l, r)`, data, where `l` is the map with all
the bindings of `m` whose 'k is strictly less than `x`; `r` is the map with
all the bindings of m whose 'k is strictly greater than `x`; `data` is
`None` if `m` contains no binding for `x`, or `Some(v)` if `m` binds `v` to
`x`.
*/
let split: (t<'k, 'v, 'id>, 'k) => ((t<'k, 'v, 'id>, t<'k, 'v, 'id>), option<'v>)
@deprecated("Use `map` instead")
let mapU: (t<'k, 'v, 'id>, 'v => 'v2) => t<'k, 'v2, 'id>
/**
`map(m, f) returns a map with same domain as`m`, where the associated
value`a`of all bindings of`m`has been replaced by the result of the
application of`f`to`a`. The bindings are passed to`f` in increasing order
with respect to the ordering over the type of the keys.
*/
let map: (t<'k, 'v, 'id>, 'v => 'v2) => t<'k, 'v2, 'id>
@deprecated("Use `mapWithKey` instead")
let mapWithKeyU: (t<'k, 'v, 'id>, ('k, 'v) => 'v2) => t<'k, 'v2, 'id>
/**
`mapWithKey(m, f)`
The same as `Belt.Map.map` except that `f` is supplied with one more
argument: the key.
*/
let mapWithKey: (t<'k, 'v, 'id>, ('k, 'v) => 'v2) => t<'k, 'v2, 'id>
/**
`getData(s0)`
Advanced usage only
Returns the raw data (detached from comparator), but its type is still
manifested, so that user can pass identity directly without boxing.
*/
let getData: t<'k, 'v, 'id> => Belt_MapDict.t<'k, 'v, 'id>
/**
Advanced usage only. Returns the identity of s0.
*/
let getId: t<'k, 'v, 'id> => id<'k, 'id>
/**
`packIdData(~id, ~data)`
Advanced usage only
Returns the packed collection.
*/
let packIdData: (~id: id<'k, 'id>, ~data: Belt_MapDict.t<'k, 'v, 'id>) => t<'k, 'v, 'id>
/**
**raise** when invariant is not held
*/
let checkInvariantInternal: t<_> => unit