23
23
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24
24
25
25
(* * {!Belt.List}
26
-
26
+
27
27
Utilities for List data type.
28
-
28
+
29
29
This module is compatible with original ocaml stdlib.
30
30
In general, all functions comes with the original stdlib also
31
31
applies to this collection, however, this module provides faster
32
- and stack safer utilities
32
+ and stack safer utilities
33
33
34
34
*)
35
35
@@ -52,11 +52,11 @@ val head: 'a t -> 'a option
52
52
head [1;2;3] = Some 1 ;;
53
53
]}
54
54
*)
55
- val headExn : 'a t -> 'a
55
+ val headExn : 'a t -> 'a
56
56
(* * [headExn h]
57
57
58
58
{b See} {!head}
59
-
59
+
60
60
{b raise} an exception if [h] is empty
61
61
62
62
*)
@@ -68,12 +68,12 @@ val tail: 'a t -> 'a t option
68
68
tail [1;2] = Some [2];;
69
69
]}
70
70
*)
71
-
72
- val tailExn : 'a t -> 'a t
71
+
72
+ val tailExn : 'a t -> 'a t
73
73
(* * [tailExn h]
74
74
75
75
{b See} {!tail}
76
-
76
+
77
77
{b raise} an exception if [h] is empty
78
78
*)
79
79
@@ -99,40 +99,40 @@ val getExn: 'a t -> int -> 'a
99
99
(* * [getExn xs n]
100
100
101
101
{b See} {!get}
102
-
102
+
103
103
{b raise} an exception if [n] is larger than the length
104
- *)
104
+ *)
105
105
106
106
val make : int -> 'a -> 'a t
107
- (* * [make n v]
108
-
109
- - return a list of length [n] with each element filled with [v]
107
+ (* * [make n v]
108
+
109
+ - return a list of length [n] with each element filled with [v]
110
110
- return the empty list if [n] is negative
111
111
112
112
@example {[
113
113
make 3 1 = [1;1;1]
114
114
]}
115
115
*)
116
-
117
- val makeByU : int -> (int -> 'a [@ bs]) -> 'a t
116
+
117
+ val makeByU : int -> (int -> 'a [@ bs]) -> 'a t
118
118
val makeBy : int -> (int -> 'a ) -> 'a t
119
- (* * [makeBy n f]
120
-
119
+ (* * [makeBy n f]
120
+
121
121
- return a list of length [n] with element [i] initialized with [f i]
122
122
- return the empty list if [n] is negative
123
123
124
124
@example {[
125
125
makeBy 5 (fun i -> i) = [0;1;2;3;4]
126
126
]}
127
- *)
127
+ *)
128
128
129
- val shuffle : 'a t -> 'a t
129
+ val shuffle : 'a t -> 'a t
130
130
(* * [shuffle xs]
131
131
@return a new list in random order
132
132
*)
133
133
134
134
135
- val drop : 'a t -> int -> 'a t option
135
+ val drop : 'a t -> int -> 'a t option
136
136
(* * [drop xs n]
137
137
138
138
return the list obtained by dropping the first [n] elements,
@@ -145,7 +145,7 @@ val drop: 'a t -> int -> 'a t option
145
145
]}
146
146
*)
147
147
148
- val take : 'a t -> int -> 'a t option
148
+ val take : 'a t -> int -> 'a t option
149
149
(* * [take xs n]
150
150
151
151
return a list with the first [n] elements from [xs],
@@ -158,17 +158,17 @@ val take: 'a t -> int -> 'a t option
158
158
]}
159
159
*)
160
160
161
- val splitAt : 'a t -> int -> ('a list * 'a list ) option
161
+ val splitAt : 'a t -> int -> ('a list * 'a list ) option
162
162
(* *
163
163
[splitAt xs n]
164
164
split the list [xs] at position [n]
165
165
return None when the length of [xs] is less than [n]
166
166
167
167
@example{[
168
168
splitAt [0;1;2;3;4] 2 = Some ([0;1], [2;3;4])
169
- ]}
169
+ ]}
170
170
*)
171
-
171
+
172
172
val concat : 'a t -> 'a t -> 'a t
173
173
(* *
174
174
[concat xs ys]
@@ -197,7 +197,7 @@ val reverseConcat: 'a t -> 'a t -> 'a t
197
197
reverseConcat [1;2] [3;4] = [2;1;3;4]
198
198
]}
199
199
*)
200
-
200
+
201
201
val flatten : 'a t t -> 'a t
202
202
(* *
203
203
[flatten ls]
@@ -236,7 +236,7 @@ val zipBy: 'a t -> 'b t -> ('a -> 'b -> 'c) -> 'c t
236
236
(* * [zipBy xs ys f]
237
237
238
238
{b See} {!zip}
239
-
239
+
240
240
Equivalent to [zip xs ys |> List.map (fun (x,y) -> f x y)]
241
241
*)
242
242
@@ -248,10 +248,10 @@ val mapWithIndex: 'a t -> (int -> 'a -> 'b) -> 'b t
248
248
]}
249
249
*)
250
250
251
- val ofArray : 'a array -> 'a t
251
+ val ofArray : 'a array -> 'a t
252
252
[@@ ocaml.deprecated "Use fromArray instead" ]
253
253
254
- val fromArray : 'a array -> 'a t
254
+ val fromArray : 'a array -> 'a t
255
255
(* * @example {[
256
256
fromArray [|1;2;3|] = [1;2;3]
257
257
]}
@@ -273,12 +273,12 @@ val reverse: 'a t -> 'a t
273
273
reverse [1;2;3] = [3;2;1]
274
274
]}
275
275
*)
276
-
276
+
277
277
val mapReverseU : 'a t -> ('a -> 'b [@ bs]) -> 'b t
278
278
val mapReverse : 'a t -> ('a -> 'b ) -> 'b t
279
279
(* * [mapReverse a f]
280
280
281
- Equivalent to [reverse (map a f)]
281
+ Equivalent to [reverse (map a f)]
282
282
*)
283
283
284
284
val forEachU : 'a t -> ('a -> 'b [@ bs]) -> unit
@@ -290,7 +290,7 @@ val forEach: 'a t -> ('a -> 'b) -> unit
290
290
!us = 1 + 2 + 3 + 4;;
291
291
]}
292
292
*)
293
-
293
+
294
294
val forEachWithIndexU : 'a t -> (int -> 'a -> 'b [@ bs]) -> unit
295
295
val forEachWithIndex : 'a t -> (int -> 'a -> 'b ) -> unit
296
296
(* * [forEachWithIndex xs f]
@@ -312,7 +312,7 @@ val reduce: 'a t -> 'b -> ('b -> 'a -> 'b) -> 'b
312
312
reduce [1;2;3;4] [] add = [4;3;2;1];
313
313
]}
314
314
*)
315
-
315
+
316
316
val reduceReverseU : 'a t -> 'b -> ('b -> 'a -> 'b [@ bs]) -> 'b
317
317
val reduceReverse : 'a t -> 'b -> ('b -> 'a -> 'b ) -> 'b
318
318
(* * [reduceReverse xs f]
@@ -323,12 +323,12 @@ val reduceReverse: 'a t -> 'b -> ('b -> 'a -> 'b) -> 'b
323
323
reduceReverse [1;2;3;4] [] add = [1;2;3;4];;
324
324
]}
325
325
*)
326
-
326
+
327
327
val mapReverse2U : 'a t -> 'b t -> ('a -> 'b -> 'c [@ bs]) -> 'c t
328
328
val mapReverse2 : 'a t -> 'b t -> ('a -> 'b -> 'c ) -> 'c t
329
329
(* * [mapReverse2 xs ys f]
330
330
331
- equivalent to [reverse (zipBy xs ys f)]
331
+ equivalent to [reverse (zipBy xs ys f)]
332
332
333
333
@example {[
334
334
mapReverse2 [1;2;3] [1;2] (+) = [4;2]
@@ -338,7 +338,7 @@ val mapReverse2: 'a t -> 'b t -> ('a -> 'b -> 'c) -> 'c t
338
338
val forEach2U : 'a t -> 'b t -> ('a -> 'b -> 'c [@ bs]) -> unit
339
339
val forEach2 : 'a t -> 'b t -> ('a -> 'b -> 'c ) -> unit
340
340
(* * [forEach2 xs ys f] stop with the shorter list
341
- *)
341
+ *)
342
342
343
343
344
344
val reduce2U :
@@ -347,7 +347,7 @@ val reduce2:
347
347
'b t -> 'c t -> 'a -> ('a -> 'b -> 'c -> 'a ) -> 'a
348
348
(* * [reduce2 xs ys init f ]
349
349
350
- stops with the shorter list.
350
+ stops with the shorter list.
351
351
*)
352
352
353
353
val reduceReverse2U :
@@ -388,7 +388,7 @@ val every2: 'a t -> 'b t -> ('a -> 'b -> bool ) -> bool
388
388
(* * [every2 xs ys p] stop with the shorter list
389
389
@example {[
390
390
(every2 [] [1] (fun x y -> x > y)) = true;;
391
- (every2 [2;3] [1] (fun x y -> x > y)) = true;;
391
+ (every2 [2;3] [1] (fun x y -> x > y)) = true;;
392
392
]}
393
393
*)
394
394
@@ -406,7 +406,7 @@ val cmpByLength: 'a t -> 'a t -> int
406
406
407
407
Compare two lists solely by length
408
408
*)
409
-
409
+
410
410
val cmpU : 'a t -> 'a t -> ('a -> 'a -> int [@ bs]) -> int
411
411
val cmp : 'a t -> 'a t -> ('a -> 'a -> int ) -> int
412
412
(* *
@@ -419,7 +419,7 @@ val cmp: 'a t -> 'a t -> ('a -> 'a -> int) -> int
419
419
420
420
{b Attention}: The total ordering of List is different from Array,
421
421
for Array, we compare the length first and one by one later, while
422
- for lists, we just compare one by one
422
+ for lists, we just compare one by one
423
423
*)
424
424
425
425
@@ -433,7 +433,7 @@ val eq: 'a t -> 'a t -> ('a -> 'a -> bool) -> bool
433
433
eq [1;2;3] [1;2] (=) = false ;;
434
434
eq [1;2] [1;2] (=) = true
435
435
]}
436
- *)
436
+ *)
437
437
438
438
439
439
val hasU : 'a t -> 'b -> ('a -> 'b -> bool [@ bs]) -> bool
@@ -450,7 +450,7 @@ val getBy: 'a t -> ('a -> bool) -> 'a option
450
450
getBy [1;4;3;2] (fun x -> x mod 2 = 0) = Some 4
451
451
]}
452
452
*)
453
-
453
+
454
454
val keepU : 'a t -> ('a -> bool [@ bs]) -> 'a t
455
455
val keep : 'a t -> ('a -> bool ) -> 'a t
456
456
(* * [keep xs p]
@@ -489,7 +489,7 @@ val unzip: ('a * 'b) t -> 'a t * 'b t
489
489
val getAssocU : ('a * 'c ) t -> 'b -> ('a -> 'b -> bool [@ bs]) -> 'c option
490
490
val getAssoc : ('a * 'c ) t -> 'b -> ('a -> 'b -> bool ) -> 'c option
491
491
(* * [getAssoc xs k eq]
492
-
492
+
493
493
return the second element of a pair in [xs] where the first element equals [x],
494
494
or [None] if not found
495
495
@example {[
@@ -517,16 +517,16 @@ val removeAssoc: ('a * 'c) t -> 'b -> ('a -> 'b -> bool) -> ('a * 'c) t
517
517
*)
518
518
519
519
val setAssocU : ('a * 'c ) t -> 'a -> 'c -> ('a -> 'a -> bool [@ bs]) -> ('a * 'c ) t
520
- val setAssoc : ('a * 'c ) t -> 'a -> 'c -> ('a -> 'a -> bool ) -> ('a * 'c ) t
520
+ val setAssoc : ('a * 'c ) t -> 'a -> 'c -> ('a -> 'a -> bool ) -> ('a * 'c ) t
521
521
(* * [setAssoc xs k v eq]
522
522
if [k] exists in [xs], replace it with the new [v], otherwise, add
523
523
it to the head
524
524
@example {[
525
525
setAssoc [1,"a"; 2, "b"; 3, "c"] 2 "x" (=) =
526
- [1,"a"; 2, "x"; 3,"c"] ;;
526
+ [1,"a"; 2, "x"; 3,"c"] ;;
527
527
528
- setAssoc [1,"a"; 3, "c"] 2 "2" (=) =
529
- [2,"2"; 1,"a"; 3, "c"]
528
+ setAssoc [1,"a"; 3, "c"] 2 "2" (=) =
529
+ [2,"2"; 1,"a"; 3, "c"]
530
530
]}
531
531
*)
532
532
@@ -536,6 +536,6 @@ val sort: 'a t -> ('a -> 'a -> int) -> 'a t
536
536
(* * [sort xs]
537
537
Returns a sorted list.
538
538
@example {[
539
- sort (fun a b -> a - b) [5; 4; 9; 3; 7] = [3; 4; 5; 7; 9]
539
+ sort [5; 4; 9; 3; 7] (fun a b -> a - b) = [3; 4; 5; 7; 9]
540
540
]}
541
541
*)
0 commit comments